观察位于另一个模块中的嵌套模块中的事件,无法识别输入更改。

huangapple go评论69阅读模式
英文:

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_serverouter_server 之间的通信:inner_server 可以在加载时引发页面更改。
  • tabsetPanel 知道 inner_ui 中的选项卡存在:page_21 按钮成功切换到 page_1

不起作用的部分:

  • inner_serverinner_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 and outer_server communication: the inner_server can cause a page change on load
  • tabsetPanel knows the tab in inner_ui exist: page_21 button successfully switches to page_1

Thing that doesn't work:

  • inner_server and inner_ui communication: the observeEvent(input$page_12... is not being triggered when the page_12 button is pressed.
library(shiny)
inner_ui &lt;- function(id) {
ns &lt;- NS(id)
tabPanel(title = &quot;page_1&quot;,
actionButton(
inputId = ns(&quot;page_12&quot;),
label = &quot;next&quot;)
)
}
inner_server &lt;- function(id, r, parent) {
moduleServer(id, function(input, output, session) {
r$page &lt;- 2
observeEvent(input$page_12, r$page &lt;- 2)
}
)}
outer_ui &lt;- function(id) {
ns &lt;- NS(id)
fluidPage(
tabsetPanel(
# we name the set of tabs so we can call on the group later
id = ns(&quot;wizard&quot;),
# this makes all the tabs except for the selected one hidden
type = &quot;hidden&quot;,
# modularized the first page tab
inner_ui(&quot;first_page&quot;),
# The other two tabs are identical to the Hadley example
tabPanel(title = &quot;page_2&quot;,
h4(&quot;Only one page to go&quot;),
actionButton(inputId = ns(&quot;page_21&quot;), label = &quot;prev&quot;),
actionButton(inputId = ns(&quot;page_23&quot;), label = &quot;next&quot;)
),
tabPanel(title = &quot;page_3&quot;,
h4(&quot;You&#39;re done!&quot;),
actionButton(inputId = ns(&quot;page_32&quot;), label = &quot;prev&quot;)
)
)
)
}
outer_server &lt;- function(id, r, parent) {
moduleServer(id, function(input, output, session) {
observeEvent(r$page, {
updateTabsetPanel(inputId = &quot;wizard&quot;, selected = paste0(&quot;page_&quot;, r$page))
})
inner_server(&quot;first_page&quot;, r = r, parent = session)
observeEvent(input$page_21, r$page &lt;- 1)
observeEvent(input$page_23, r$page &lt;- 3)
observeEvent(input$page_32, r$page &lt;- 2)
}
)
}
# Define the tabsetPanel module
ui &lt;- navbarPage(
title = NULL,
tabPanel(title = &quot;test Wizard&quot;,
outer_ui(&quot;test_outer&quot;))
)
server &lt;- function(input, output, session) {
r &lt;- reactiveValues()
outer_server(&quot;test_outer&quot;, 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(&quot;first_page&quot;) to inner_ui(ns(&quot;first_page)) in the inner_ui function fixed the issue.

huangapple
  • 本文由 发表于 2023年8月4日 05:32:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定