英文:
oberveEvent on an action located in another module
问题
在一个闪亮的应用程序中,我有两个模块,在第一个模块(mod_1_ui)中我有筛选器和一个按钮。在模块2中,我希望在按钮被点击时显示一个绘图。
我的问题是,observeEvent位于模块2中,而按钮位于模块1中。如何观察两个不同模块中的按钮点击事件?
以下是一个可重现的示例:
mod_1_ui = function(id) {
ns = NS(id)
fluidRow(
column(
width = 3,
radioButtons(
ns("dist"),
"Distribution type:",
c(
"Normal" = "norm",
"Uniform" = "unif",
"Log-normal" = "lnorm",
"Exponential" = "exp"
)
),
actionButton(
inputId = ns("btn_go"),
label = "Go !",
icon = icon("play")
)
),
column(width = 9,
mod_2_ui(ns("display_plot"))
)
)
}
mod_1_server = function(id, r_global) {
moduleServer(id, function(input, output, session) {
mod_2_server("display_plot")
})
}
mod_2_ui = function(id) {
ns = NS(id)
plotOutput(outputId = ns("plot"))
}
mod_2_server = function(id, r_global) {
moduleServer(id, function(input, output, session) {
observeEvent(input$btn_go,{
output$plot <- renderPlot({
plot(mtcars$mpg)
})
})
})
}
ui <- fluidPage(
mod_1_ui("mod")
)
server <- function(input, output, session) {
r_global <- reactiveValues()
mod_1_server("mod", r_global = r_global)
}
if (interactive())
shinyApp(ui, server)
英文:
In a shiny app, I have 2 modules, in the first one (mod_1_ui) I have filters and an actionButton.
On the module 2, I want to display a plot WHEN button is clicked.
My problem is that, the observeEvent is in module 2 and the button in module 1.
How can I observeEvent an action in 2 differents modules ?
Below is a reproducible example :
mod_1_ui = function(id) {
ns = NS(id)
fluidRow(
column(
width = 3,
radioButtons(
ns("dist"),
"Distribution type:",
c(
"Normal" = "norm",
"Uniform" = "unif",
"Log-normal" = "lnorm",
"Exponential" = "exp"
)
),
actionButton(
inputId = ns("btn_go"),
label = "Go !",
icon = icon("play")
)
),
column(width = 9,
mod_2_ui(ns("display_plot"))
)
)
}
mod_1_server = function(id, r_global) {
moduleServer(id, function(input, output, session) {
mod_2_server("display_plot")
})
}
mod_2_ui = function(id) {
ns = NS(id)
plotOutput(outputId = ns("plot"))
}
mod_2_server = function(id, r_global) {
moduleServer(id, function(input, output, session) {
observeEvent(input$btn_go,{
output$plot <- renderPlot({
plot(mtcars$mpg)
})
})
})
}
ui <- fluidPage(
mod_1_ui("mod")
)
server <- function(input, output, session) {
r_global <- reactiveValues()
mod_1_server("mod", r_global = r_global)
}
if (interactive())
shinyApp(ui, server)
答案1
得分: 1
以下是代码的翻译部分:
一种选项是使用reactive
或reactiveVal
将按钮的状态作为参数传递给模块2的服务器:
library(shiny)
mod_1_ui <- function(id) {
ns <- NS(id)
fluidRow(
column(
width = 3,
radioButtons(
ns("dist"),
"Distribution type:",
c(
"Normal" = "norm",
"Uniform" = "unif",
"Log-normal" = "lnorm",
"Exponential" = "exp"
)
),
actionButton(
inputId = ns("btn_go"),
label = "Go !",
icon = icon("play")
)
),
column(
width = 9,
mod_2_ui(ns("display_plot"))
)
)
}
mod_1_server <- function(id, r_global) {
moduleServer(id, function(input, output, session) {
show <- reactive({
input$btn_go
})
mod_2_server("display_plot", show = show)
})
}
mod_2_ui <- function(id) {
ns <- NS(id)
plotOutput(outputId = ns("plot"))
}
mod_2_server = function(id, r_global, show) {
moduleServer(id, function(input, output, session) {
observeEvent(show(),{
output$plot <- renderPlot({
plot(runif(10, runif(10)))
})
})
})
}
ui <- fluidPage(
mod_1_ui("mod")
)
server <- function(input, output, session) {
r_global <- reactiveValues()
mod_1_server("mod", r_global = r_global)
}
if (interactive()) {
shinyApp(ui, server)
}
英文:
One option would be to use a reactive
or a reactiveVal
to pass the state of your button as an argument to the module 2 server:
library(shiny)
mod_1_ui <- function(id) {
ns <- NS(id)
fluidRow(
column(
width = 3,
radioButtons(
ns("dist"),
"Distribution type:",
c(
"Normal" = "norm",
"Uniform" = "unif",
"Log-normal" = "lnorm",
"Exponential" = "exp"
)
),
actionButton(
inputId = ns("btn_go"),
label = "Go !",
icon = icon("play")
)
),
column(
width = 9,
mod_2_ui(ns("display_plot"))
)
)
}
mod_1_server <- function(id, r_global) {
moduleServer(id, function(input, output, session) {
show <- reactive({
input$btn_go
})
mod_2_server("display_plot", show = show)
})
}
mod_2_ui <- function(id) {
ns <- NS(id)
plotOutput(outputId = ns("plot"))
}
mod_2_server = function(id, r_global, show) {
moduleServer(id, function(input, output, session) {
observeEvent(show(),{
output$plot <- renderPlot({
plot(runif(10, runif(10)))
})
})
})
}
ui <- fluidPage(
mod_1_ui("mod")
)
server <- function(input, output, session) {
r_global <- reactiveValues()
mod_1_server("mod", r_global = r_global)
}
if (interactive()) {
shinyApp(ui, server)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论