【发布时间】:2020-06-22 11:17:42
【问题描述】:
我正在尝试使用 Rshiny 和 SIR() 模型创建交互式绘图。基本上,我想要 5 个滑块输入,它们是 beta、gamma、S、I 和 R,并在指定时间内输出 S、I、R 的图。
以下是我在 Rstudio 中的代码。忽略 S+I+R 应该等于 1 的事实。
有什么错误的建议吗?
ui <- fluidPage(
# App title ----
titlePanel("Sliders"),
sliderInput(inputId = "gr1", "Beta:",
min = 0, max = 3,
value = 1.42),
sliderInput(inputId = "gr2", "Gamma:",
min = 0, max = 1,
value = 0.142),
sliderInput(inputId = "gr3", "S proportion:",
min = 0, max = 1,
value = 1 - 1e-06),
sliderInput(inputId = "gr4", "I proportion:",
min = 0, max = 1,
value = 1e-06),
sliderInput(inputId = "gr5", "R proportion:",
min = 0, max = 1,
value = 1e-06 + 1e-06),
plotOutput("plot")
)
server <- function(input, output){
val <- SIR(pars = c(input$gr1,input$gr2), init = c(input$gr3,input$gr4,input$gr5), time = 0:70)$results
output$plot <- renderPlot({
with(val, {
plot(time, S, type = "l", col = "blue",
xlab = "period (days)", ylab = "Proportion of people")
lines(time, I, col = "red")
lines(time, R, col = "green")
})
legend("right", c("susceptibles ratio", "infectious ratio", "recovered ratio"),
col = c("blue", "red", "green"), lty = 1, bty = "n")
})
}
shinyApp(ui = ui, server = server)
【问题讨论】:
-
什么是SIR函数?
-
你能提供你得到的错误吗?
-
下面使用 reactive() 的解决方案是正确的。非常感谢
标签: r