假设我有以下 Shiny 的server.R文件:
shinyServer(function(input, output) {
output$plot <- renderPlot({
data2 <- data[data$x == input$z, ] # subsetting large dataframe
plot(data2$x, data2$y)
})
output$table <- renderTable({
data2 <- data[data$x == input$z, ] # same subset. Oh, boy...
summary(data2$x)
})
})
为了不必在每个渲染调用中都运行
data2 <- data[data$x == input$z, ]
,我该怎么办?如果执行以下操作,则会出现“'closure'类型的对象不可子集化”错误:shinyServer(function(input, output) {
data2 <- reactive(data[data$x == input$z, ])
output$plot <- renderPlot({
plot(data2$x, data2$y)
})
output$table <- renderTable({
data2 <- data[data$x == input$z, ]
summary(data2$x)
})
})
我做错了什么?
最佳答案
data2
是一个函数,它返回您要寻找的子集。因此,您需要调用data2
并将输出保存到某个变量中,然后才能绘制/汇总各个列
## data should be defined somewhere up here or in global.R
shinyServer(function(input, output) {
data2 <- reactive(data[data$x == input$z, ])
output$plot <- renderPlot({
newData <- data2()
plot(newData$x, newData$y)
})
output$table <- renderTable({
newData <- data2()
summary(newData$x)
})
})
如果您还没有的话,建议您阅读http://rstudio.github.io/shiny/tutorial/#welcome。关于 react 性的页面很好地解决了这个问题。
关于r - 在Shiny中设置全局对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17683933/