1

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

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

谢谢你。

应用程序.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)

模块.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
  })
}
4

1 回答 1

1

问题中似乎缺少第二个模块,但逻辑似乎很简单。这里的问题是您在使用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()or 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)
于 2019-04-11T01:46:18.103 回答