【问题标题】:shiny module namespace issue with dynamic creation of tabesetPanel动态创建 tabsetPanel 的闪亮模块命名空间问题
【发布时间】:2022-01-04 10:07:14
【问题描述】:

我目前在使我的模块 UI 和服务器与创建布局的中间 renderUI 通信时遇到问题。这是一个有和没有动态创建 tabsetPanel 的代表。我猜这个问题来自命名空间,但我不知道在哪里以及如何解决它。 不要工作:

mod_graphical_general_ui <- function(id){
  ns <- NS(id)
  tagList(
    selectInput(ns("myselect"), "Select a choice", choices = NULL)
)}

mod_graphical_general_server <- function(id, choices = NULL) {
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    updateSelectInput(session, "myselect", choices = choices)
    
  })
}

ui <- bootstrapPage(
  uiOutput("mytabs")
)

server <- function(input, output) {
  mod_graphical_general_server("mymodule", choices = c("aaa", "bbb"))
  
  output$mytabs = renderUI({
    number_of_tabs <- 3
    names_tab <- paste0("Tab", 1:number_of_tabs)
    myTabs = lapply(1: number_of_tabs, function(x) {tabPanel(names_tab[[x]], div(uiOutput(paste0("graphics_tab", x))))})
    do.call(tabsetPanel, c(myTabs))
  })
  
  output$graphics_tab1 <- renderUI({
    return(mod_graphical_general_ui("mymodule"))
  })
}

shinyApp(ui = ui, server = server)

如果我从调用 tabsetPanel 中删除该步骤,则代码有效。

mod_graphical_general_ui <- function(id){
  ns <- NS(id)
  tagList(
    selectInput(ns("myselect"), "Select a choice", choices = NULL)
)}

mod_graphical_general_server <- function(id, choices = NULL) {
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    updateSelectInput(session, "myselect", choices = choices)
    
  })
}

ui <- bootstrapPage(
  #uiOutput("mytabs")
  uiOutput("graphics_tab1")
)

server <- function(input, output) {
  mod_graphical_general_server("mymodule", choices = c("aaa", "bbb"))
  
  output$mytabs = renderUI({
    number_of_tabs <- 3
    names_tab <- paste0("Tab", 1:number_of_tabs)
    myTabs = lapply(1: number_of_tabs, function(x) {tabPanel(names_tab[[x]], div(uiOutput(paste0("graphics_tab", x))))})
    do.call(tabsetPanel, c(myTabs))
  })
  
  output$graphics_tab1 <- renderUI({
    return(mod_graphical_general_ui("mymodule"))
  })
}

shinyApp(ui = ui, server = server)

我已经在社区 rstudio 中问过这个问题,但没有运气。

【问题讨论】:

  • 第一个例子和第二个例子不一样吗?

标签: r shiny shinymodules


【解决方案1】:

不管怎样,我已经修复了你的代码。

问题是您的 mod 服务器在顶级闪亮服务器启动时运行。但是,您的 mod UI 稍后会在 mod 服务器之后运行。所以这会导致updateSelectInput 找不到要更新的动态 UI 组件。在您的第二个示例中,UI 组件在应用启动时已经存在,因此不存在此问题。

当我们可以调用 mod 服务器时,我们需要等待渲染 UI 事件完成。要理解这一点,您需要了解 Shiny 如何与前端 javascript 通信,此处不再赘述。您可以在this issue 上阅读更多内容。

mod_graphical_general_ui <- function(id){
  ns <- NS(id)
  tagList(
    selectInput(ns("myselect"), "Select a choice", choices = NULL)
  )}

mod_graphical_general_server <- function(id, choices = NULL) {
  moduleServer(id, function(input, output, session){
    ns <- session$ns
    updateSelectInput(session, "myselect", choices = choices)
    
  })
}

ui <- bootstrapPage(
  uiOutput("mytabs")
)

server <- function(input, output, session) {
  output$mytabs = renderUI({
    number_of_tabs <- 3
    names_tab <- paste0("Tab", 1:number_of_tabs)
    myTabs = lapply(1:number_of_tabs, function(x) {tabPanel(names_tab[[x]], div(uiOutput(paste0("graphics_tab", x))))})
    do.call(tabsetPanel, c(myTabs))
  })
  
  output$graphics_tab1 <- renderUI({
    on.exit({
      observeEvent(once = TRUE, reactiveValuesToList(session$input), {
        mod_graphical_general_server("mymodule", choices = c("aaa", "bbb"))
      }, ignoreInit = TRUE)
    })
    return(mod_graphical_general_ui("mymodule"))
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 2017-12-23
    • 2020-12-11
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多