在 R Shiny 应用程序中将附加参数传递给 moduleServer:访问 updateTabsetPanel 的父环境

hed*_*ds1 9 r shiny shinymodules

  • 从 Shiny 1.5.0 开始,我们鼓励使用moduleServer而不是callModule.
  • 但是,我们似乎无法将其他参数传递给moduleServer(与callModule)不同。
  • 这是一个问题,因为我想将父会话作为参数传递给moduleServer,以便我可以正确引用父会话,以便updateTabsetPanel在动态输出中正常工作renderUI

这篇文章演示了如何使用callModule将父会话传递到模块中,以便updateTabsetPanel可以引用父会话并相应地更新它。我在这里使用这种方法创建了一个最小的示例。有两个tabPanels,我们试图通过actionLink第一个中的 an 导航到第二个。该first_server模块是用 调用的callModule,我们可以将父会话参数作为附加参数传递,从而允许我们在 的updateTabsetPanel调用中引用它first_server。单击actionLinkthen 会将我们带到第二个模块 UI,如预期的那样:

library(shiny)

first_ui <- function(id) {
    ns <- NS(id)
    uiOutput(ns("goto_second_link_ui"))
}

second_ui <- function(id) {
    ns <- NS(id)
    p("It worked!")
}

first_server <- function(input, output, session, parent) {
    output$goto_second_link_ui <- renderUI({
        actionLink(session$ns("goto_second"), label = "Go to second module")
    })
    observeEvent(input$goto_second, {
        updateTabsetPanel(parent,
            inputId = "main",
            selected = "second"
        )
    })
}

ui <- {
    fluidPage(
        tabsetPanel(id = "main",
            tabPanel(value = "first",
                title = "First module",
                first_ui("first")    
            ),
            tabPanel(value = "second",
                title = "Second module",
                second_ui("second")    
            )
        )
    )
}

server <- function(input, output, session) {
    callModule(first_server, id = "first", parent = session)
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

我尝试了几种方法来让它与moduleServer. 我最好的猜测是将父会话一直传递到参数modulemoduleServer仅显示相关更改):

first_server <- function(id, parent) {
    moduleServer(id, function(input, output, session, parent = parent) {
        output$goto_second_link_ui <- renderUI({
            actionLink(session$ns("goto_second"), label = "Go to second module")
        })
        observeEvent(input$goto_second, {
            updateTabsetPanel(parent,
                inputId = "main",
                selected = "second"
            )
        })
    })
}

server <- function(input, output, session) {
    first_server("first", parent = session)
}
Run Code Online (Sandbox Code Playgroud)

actionLink当我单击我不完全理解的时,这会生成错误:

警告:updateTabsetPanel 中的错误:promise 已在评估中:递归默认参数引用或早期问题?

感谢您的阅读和任何建议。

YBS*_*YBS 9

一旦你删除了额外的参数,moduleServer它就起作用了。尝试这个

library(shiny)

first_ui <- function(id) {
  ns <- NS(id)
  uiOutput(ns("goto_second_link_ui"))
}

second_ui <- function(id) {
  ns <- NS(id)
  p("It worked!")
}

first_server <- function(id,parent) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    output$goto_second_link_ui <- renderUI({
      actionLink(ns("goto_second"), label = "Go to second module")
    })
    observeEvent(input$goto_second, {
      updateTabsetPanel(parent,
                        inputId = "main",
                        selected = "second"
      )
    })
  })
}

ui <- {
  fluidPage(
    tabsetPanel(id = "main",
                tabPanel(value = "first",
                         title = "First module",
                         first_ui("first")    
                ),
                tabPanel(value = "second",
                         title = "Second module",
                         second_ui("second")    
                )
    )
  )
}

server <- function(input, output, session) {
  first_server("first", parent = session)
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)