R 用反应性数据替换非反应性数据

R 用反应性数据替换非反应性数据,r,shiny,R,Shiny,我有一个复杂的闪亮应用程序,我在其中使用了“你可以做这样的事情: 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) observeEven

我有一个复杂的闪亮应用程序,我在其中使用了“你可以做这样的事情:

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)
}

ui您不能在非反应性环境中处理像这样的反应性值。换句话说,在
observe()
或类似的内容中这样做。很好。如果它回答了你的问题,考虑一下。在代码的某个地方,我只使用了<代码>()>代码>,而不是<代码>所有的$d<代码>,这就造成了以下错误:某个东西中的错误正在试图访问你的变量,但是它并没有保存它的数据。(可能是
d
# 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)
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)
}