英文:
Observe event within module nested in another module isn't recognizing input change
问题
我有一个大型的向导应用程序,我想通过将每个隐藏的选项卡放在它们自己的模块中来拆分。
我基于Hadley的向导示例创建了下面的reprex。
布局如下:
ui
L outer_ui
L inner_ui
server
L outer_server
L inner_server
有效的部分:
inner_server
和outer_server
之间的通信:inner_server
可以在加载时引发页面更改。tabsetPanel
知道inner_ui
中的选项卡存在:page_21
按钮成功切换到page_1
。
不起作用的部分:
inner_server
和inner_ui
之间的通信:当按下page_12
按钮时,observeEvent(input$page_12...)
没有被触发。
library(shiny)
inner_ui <- function(id) {
ns <- NS(id)
tabPanel(title = "page_1",
actionButton(
inputId = ns("page_12"),
label = "next")
)
}
inner_server <- function(id, r, parent) {
moduleServer(id, function(input, output, session) {
r$page <- 2
observeEvent(input$page_12, r$page <- 2)
})
}
outer_ui <- function(id) {
ns <- NS(id)
fluidPage(
tabsetPanel(
# 我们为选项卡集合命名,以便稍后调用该组
id = ns("wizard"),
# 这会使除所选选项卡之外的所有选项卡都处于隐藏状态
type = "hidden",
# 模块化的第一页选项卡
inner_ui("first_page"),
# 其他两个选项卡与 Hadley 示例相同
tabPanel(title = "page_2",
h4("只剩下一页了"),
actionButton(inputId = ns("page_21"), label = "prev"),
actionButton(inputId = ns("page_23"), label = "next")
),
tabPanel(title = "page_3",
h4("完成了!"),
actionButton(inputId = ns("page_32"), label = "prev")
)
)
)
}
outer_server <- function(id, r, parent) {
moduleServer(id, function(input, output, session) {
observeEvent(r$page, {
updateTabsetPanel(inputId = "wizard", selected = paste0("page_", r$page))
})
inner_server("first_page", r = r, parent = session)
observeEvent(input$page_21, r$page <- 1)
observeEvent(input$page_23, r$page <- 3)
observeEvent(input$page_32, r$page <- 2)
})
}
# 定义 tabsetPanel 模块
ui <- navbarPage(
title = NULL,
tabPanel(title = "test Wizard",
outer_ui("test_outer"))
)
server <- function(input, output, session) {
r <- reactiveValues()
outer_server("test_outer", r = r, parent = session)
}
# 运行 Shiny 应用程序
shinyApp(ui, server)
英文:
I have a large wizard app that I'd like to break up by putting each of the hidden tabs within their own modules.
I've made the reprex below based off of Hadleys wizard example.
The layout is
ui
L outer_ui
L inner_ui
server
L outer_server
L inner_server
Things that do work:
inner_server
andouter_server
communication: theinner_server
can cause a page change on loadtabsetPanel
knows the tab ininner_ui
exist:page_21
button successfully switches topage_1
Thing that doesn't work:
inner_server
andinner_ui
communication: theobserveEvent(input$page_12...
is not being triggered when thepage_12
button is pressed.
library(shiny)
inner_ui <- function(id) {
ns <- NS(id)
tabPanel(title = "page_1",
actionButton(
inputId = ns("page_12"),
label = "next")
)
}
inner_server <- function(id, r, parent) {
moduleServer(id, function(input, output, session) {
r$page <- 2
observeEvent(input$page_12, r$page <- 2)
}
)}
outer_ui <- function(id) {
ns <- NS(id)
fluidPage(
tabsetPanel(
# we name the set of tabs so we can call on the group later
id = ns("wizard"),
# this makes all the tabs except for the selected one hidden
type = "hidden",
# modularized the first page tab
inner_ui("first_page"),
# The other two tabs are identical to the Hadley example
tabPanel(title = "page_2",
h4("Only one page to go"),
actionButton(inputId = ns("page_21"), label = "prev"),
actionButton(inputId = ns("page_23"), label = "next")
),
tabPanel(title = "page_3",
h4("You're done!"),
actionButton(inputId = ns("page_32"), label = "prev")
)
)
)
}
outer_server <- function(id, r, parent) {
moduleServer(id, function(input, output, session) {
observeEvent(r$page, {
updateTabsetPanel(inputId = "wizard", selected = paste0("page_", r$page))
})
inner_server("first_page", r = r, parent = session)
observeEvent(input$page_21, r$page <- 1)
observeEvent(input$page_23, r$page <- 3)
observeEvent(input$page_32, r$page <- 2)
}
)
}
# Define the tabsetPanel module
ui <- navbarPage(
title = NULL,
tabPanel(title = "test Wizard",
outer_ui("test_outer"))
)
server <- function(input, output, session) {
r <- reactiveValues()
outer_server("test_outer", r = r, parent = session)
}
# Run the Shiny app
shinyApp(ui, server)
答案1
得分: 0
我能够从Ben在这个问题的评论中找到答案。
将inner_ui("first_page")
更改为inner_ui(ns("first_page))
在inner_ui
函数中修复了问题。
英文:
I was able to find the answer from Ben's comment on this question.
Changing inner_ui("first_page")
to inner_ui(ns("first_page))
in the inner_ui
function fixed the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论