我是R Shiny的新手,所以请耐心等待我。我正在尝试创建一个交互式模型,其工作原理如下:
理想情况下,它看起来像这样:
server <- function(input, output, session){
observeEvent(input$button1, {
# generate the model, model <- lm(...)
# pass general information about the model to the UI (R's "summary")
})
observeEvent(input$button2, {
# create predictions using model and output them to the UI
})
}
但当然模型超出了范围,不能在第二个observeEvent中使用。我只想弄清楚如何解决这个问题,而我似乎无法找到答案。
答案 0 :(得分:0)
正如评论中所建议的那样,尝试使您的模型成为反应函数,而不是observeEvent
:
model <- eventReactive(input$button1, {
# generate the model, tempvariable <- lm(...)
# pass general information about the model to the UI (R's "summary")
# Put the model in the last line so that it becomes the return value of the reactive function
tempvariable
})
observeEvent(input$button2, {
# create predictions using model and output them to the UI
# here you can refer to the model by calling it model(),
# e.g., anova(model()) or temp <- model(), anova(temp)
})