我有一个复杂的闪亮应用程序,在其中我使用'<-'在非反应数据上创建新列。
# Without reactive expression
ui <- fluidPage(tableOutput("contents"))
server1 <- function(input, output) {
myData <- mtcars
myData$New1 <- rep("NV1", nrow(myData))
myData$New2 <- rep("NV2", nrow(myData))
myData$New3 <- rep("NV3", nrow(myData))
myData$New4 <- rep("NV4", nrow(myData))
output$contents <- renderTable(myData)
}
shinyApp(ui, server1)
现在,我想将非反应性数据替换为反应性。如何以最小的变化做到这一点? 我已经尝试过类似的东西:
# With reactive expression
ui <- fluidPage(tableOutput("contents"))
server2 <- function(input, output) {
all <- reactiveValues()
all$d <- mtcars
all$d$New1 <- rep("NV1", nrow(all$d))
all$d$New2 <- rep("NV2", nrow(all$d))
all$d$New3 <- rep("NV3", nrow(all$d))
all$d$New4 <- rep("NV4", nrow(all$d))
output$contents <- renderTable(all$d)
}
shinyApp(ui, server2)
答案 0 :(得分:0)
您可以执行以下操作:
ui <- fluidPage(tableOutput("contents"))
server2 <- function(input, output) {
# you can assign variables in reactiveValues(), no need to make a new line
all <- reactiveValues(d = mtcars)
observeEvent(all$d, { # update all$d, after it is created
req(!is.null(all$d))
all$d$New1 <- rep("NV1", nrow(all$d))
all$d$New2 <- rep("NV2", nrow(all$d))
all$d$New3 <- rep("NV3", nrow(all$d))
all$d$New4 <- rep("NV4", nrow(all$d))
})
output$contents <- renderTable(all$d)
}