使闪亮的模块具有反应性

时间:2019-04-11 00:07:38

标签: r shiny shiny-reactivity shinymodules

下面的示例应用程序有两个闪亮的模块。第一个模块显示一个带有随机生成值的表格以及一个操作按钮,单击该按钮可生成新值。第二个模块显示第一个模块中生成的数据集。

如何使第二个表与第一个表一起更改?

谢谢。

app.R

library(shiny)
source("modules.R")

ui <- fluidPage(

  fluidRow(
    column(6, table1_moduleUI("table1")),
    column(6, table2_moduleUI("table2"))

  )

)

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

  table1 <- callModule(table1_module, "table1")
  callModule(table2_module, "table2", table_data = table1$values)
}

shinyApp(ui, server)

modules.R

# Module for table 1

table1_moduleUI <- function(id){
  ns <- NS(id)

  tagList(
    tableOutput(ns("table")),
    actionButton(ns("submit"), label = "Change values")
  )
}

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

  table <- reactiveValues()

  observeEvent(input$submit, {
    table$values <- replicate(3, rnorm(10))
  }, ignoreNULL = FALSE)

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

  return(table)
}

# Module for table 2

table2_moduleUI <- function(id){

  ns <- NS(id)
  tableOutput(ns("table"))
}

table2_module <- function(input, output, session, table_data){

  output$table <- renderTable({
    table_data
  })
}

1 个答案:

答案 0 :(得分:0)

该问题似乎缺少第二个模块,但是逻辑似乎很简单。这里的问题是您在使用时将反应表达式的value传递给第二个模块

callModule(table2_module, "table2", table_data = table1$values)

相反,您希望传递无功值,当无功值改变时,它告诉R使输出无效,

callModule(table2_module, "table2", table_data = table1)

这是完整的应用程序,

library(shiny)

# Module for table 1

table1_moduleUI <- function(id){
  ns <- NS(id)

  tagList(
    tableOutput(ns("table")),
    actionButton(ns("submit"), label = "Change values")
  )
}

table2_moduleUI <- function(id){
  ns <- NS(id)
  tableOutput(ns("table"))
}

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

  table <- reactiveValues()

  observeEvent(input$submit, {
    table$values <- replicate(3, rnorm(10))
  }, ignoreNULL = FALSE)

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

  return(table)
}

table2_module <- function(input, output, session,table_data) {

  output$table <- renderTable({
    table_data
  })

}


ui <- fluidPage(

  fluidRow(
    column(6, table1_moduleUI("table1")),
    column(6, table2_moduleUI("table2"))
  )

)

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

  table1 <- callModule(table1_module, "table1")
  callModule(table2_module, "table2", table_data = table1$values)
}

shinyApp(ui, server)

需要注意的是,如果要显示数据框,则使用反应性值似乎是过分的。当我们要返回反应式表达式,初始化反应式变量并将其设置在观察者中时,我们可以简单地使用reactive()eventReactive(),这就是它们的作用!因此,让我们使用它。反应性值有其空间,以我的经验,使用率相对较低。

library(shiny)

# Module for table 1

table1_moduleUI <- function(id){
  ns <- NS(id)

  tagList(
    tableOutput(ns("table")),
    actionButton(ns("submit"), label = "Change values")
  )
}

table2_moduleUI <- function(id){
  ns <- NS(id)
  tableOutput(ns("table"))
}

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

  table = eventReactive(input$submit, {
    replicate(3, rnorm(10))
  }, ignoreNULL = FALSE)

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

  return(table)
}

table2_module <- function(input, output, session,table_data) {

  output$table <- renderTable({
    table_data()
  })

}


ui <- fluidPage(

  fluidRow(
    column(6, table1_moduleUI("table1")),
    column(6, table2_moduleUI("table2"))
  )

)

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

  table1 <- callModule(table1_module, "table1")
  callModule(table2_module, "table2", table_data = table1)
}

shinyApp(ui, server)