首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使闪亮的模块具有反应性

使闪亮的模块具有反应性
EN

Stack Overflow用户
提问于 2019-04-11 08:07:38
回答 1查看 171关注 0票数 1

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

如何使第二个表与第一个表发生变化?

谢谢。

app.R

代码语言:javascript
复制
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

代码语言:javascript
复制
# 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
  })
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-11 09:46:18

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

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

相反,您希望传递反应值,该值告诉R在反应值更改时使输出无效,

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

这是完整的应用程序,

代码语言:javascript
复制
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(),这就是它们的作用!因此,让我们使用它。反应值有自己的空间,在我的经验中相对较少使用。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55622915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档