由闪亮模块返回的Rbind数据到reactiveValues对象

时间:2018-03-31 08:13:42

标签: r shiny

我在我的应用程序中使用闪亮的模块,将数据返回到主应用程序。然后,该数据应该被绑定到存储在reactiveValues对象中的现有data.frame。

简化不工作的例子:

library(shiny)

data <- data.frame(x = 1:10)

moduleUI <- function(id) {
  ns <- NS(id)
  actionButton(ns("append"), "Append row")
}

module <- function(input, output, session) {
  values2 <- reactiveValues(new_row = NULL)
  observeEvent(input$append, {
    values2$new_row <- data.frame(x = sample(1:100, 1))
  })
  return(values2$new_row)
}

ui <- fluidPage(
  moduleUI("mod"),
  tableOutput("table")
)

server <- function(input, output, session) {

  values <- reactiveValues(data = data)

  x <- callModule(module, "mod")

  observeEvent(x, {
    values$data <- rbind(values$data, x)
  })

  output$table <- renderTable({
    values$data
  })
}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

<强> TL;博士

return(values2$new_row)替换为return(reactive({values2$new_row}))函数中的module,将x替换为x()observeEvent内的server

你可以举例here

library(shiny)

options(shiny.error=recover)

data <- data.frame(x = 1:10)

moduleUI <- function(id) {
  ns <- NS(id)
  actionButton(ns("append"), "Append row")
}

module <- function(input, output, session) {

  values2 <- reactiveValues(new_row = NULL)

  observeEvent(input$append, {

    values2$new_row <- data.frame(x = sample(1:100, 1))
  })

  return(reactive({values2$new_row}))
}

ui <- fluidPage(
  moduleUI("mod"),
  tableOutput("table")
)

server <- function(input, output, session) {

  values <- reactiveValues(data = data)

  x <- callModule(module, "mod")

  observeEvent(x(), {
    values$data <- rbind(values$data, x())
  })

  output$table <- renderTable({
    values$data
  })
}

shinyApp(ui, server)

如果您想了解我如何达到上述目的,请参阅以下内容:

运行代码时,我们收到以下消息:

Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
  61: stop
  60: .getReactiveEnvironment()$currentContext
  59: .subset2(x, "impl")$get
  58: $.reactivevalues
  56: module [#10]
  51: callModule
  50: server [#5]
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

这表示我们在server函数的第5行(x <- callModule(module, "mod")的行)和module函数的第10行(return(values2$new_row)的行中遇到问题})。

browser()放在module函数的第9行并再次运行代码会使浏览器在return(values2$new_row)之前停止。在浏览器中运行这段代码会产生错误消息:

Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

这意味着无论你返回什么,都应该在反应中或观察功能的一部分内。所以,我们写return(reactive({values2$new_row}))并按Enter键以查看它是否有效(即它没有给出任何错误,我们的调试器转到module函数的第10行)。

键入Q并按Enter键退出浏览器模式并返回我们的代码。输入正确的代码return(reactive({values2$new_row}))

但并非全部。由于我们的功能现在返回了无效值,因此我们必须使用x来调用x()。因此,在observeEvent位中更改此值会产生:

  observeEvent(x(), {
    values$data <- rbind(values$data, x())
  })