r - 使闪亮的模块反应
问题描述
下面的示例应用程序有两个闪亮的模块。第一个模块显示一个包含随机生成值的表格以及一个操作按钮,单击该按钮会生成新值。第二个模块显示在第一个模块中生成的数据集。
如何使第二个表与第一个表一起更改?
谢谢你。
应用程序.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
})
}
解决方案
问题中似乎缺少第二个模块,但逻辑似乎很简单。这里的问题是您在使用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)
推荐阅读
- visual-studio - Uncaught (in promise) TypeError: Cannot read properties of null (reading 'error')
- flutter - 带有 one_signal 通知的 flutter_app_badger
- android-studio - IDE 问题:无论如何,我的班级功能是否有层次结构视图?
- playwright - 使用 JS 的编剧
- memory - Julia中是否有像pymem这样的库/模块?
- python - Opencv bf.knnmatch 断言失败
- python - 如何根据 y 值对 hvplot 中的 x 轴进行排序?
- docker - docker-compose up -d 不在分离模式下工作
- google-sheets - 合并两列过滤唯一值
- episerver - Episerver-在嵌套块中查找父块