【问题标题】:How to correct namespace for renderText in nested shiny modules?如何在嵌套闪亮模块中更正 renderText 的命名空间?
【发布时间】:2017-09-18 02:38:19
【问题描述】:

我找不到在嵌套 UI 模块中使用 renderText 中的输出变量的方法。

当我试图获取选项卡的名称并将其放在我的 navbarPage 布局的标题标题中时,问题就出现了。

在解释如何通过session$ns like this one 处理renderUI(xxInput()) 的答案中,我找不到解决我的问题的方法。由于renderText 没有id 参数,我不知道这是否仍然适用。

小例子:

library(shiny)

header_module <- function(id, label="My Header") {
  ns <- NS(id)
  headerPanel(
    div(
      h2("Some text", shiny::br()),
      #h2(textOutput("tab-name")),
      h2(textOutput(ns("tab-name"))),
      h2("Some more static text")
    )
  )
}

ui <- function(id, label="Main UI") {
  ns <- NS(id)
  navbarPage(title = "My app", 
             tabPanel(title = "My tab 1", 
                      pageWithSidebar(
                        header_module(ns("header")),
                        sidebarPanel(h2("Sidebar")),
                        mainPanel(h2("Main Panel"),
                                  textOutput("tab-name"))),
                      value = "Tab1"),
             id = ns("active-tab"))
}

server <- function(input, output, session) {
  output[["tab-name"]] <- renderText({ input[["mainUI-active-tab"]] })

}

shinyApp(ui=ui("mainUI"),server=server)

对“活动标签”的引用在主面板中起作用,而不是在模块创建的标题中。如果我使用textOutput(ns("tab-name"))(如上),则只会创建一个空 div &lt;div id="mainUI-header-tab-name" class="shiny-text-output shiny-bound-output"&gt;&lt;/div&gt;。如果我使用textOutput("tab-name"),主面板也会变空。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您的代码中的问题似乎与模块无关。这是一个固定版本:

    library(shiny)
    
    header_module <- function(id, label="My Header") {
      ns <- NS(id)
      headerPanel(
        div(
          h2("Some text", shiny::br()),
          h2(textOutput("tab-name2")),
          #h2(textOutput(ns("tab-name2"))),
          h2("Some more static text")
        )
      )
    }
    
    ui <- function(id, label="Main UI") {
      ns <- NS(id)
      navbarPage(title = "My app", 
                 tabPanel(title = "My tab 1", 
                          pageWithSidebar(
                            header_module(ns("header")),
                            sidebarPanel(h2("Sidebar")),
                            mainPanel(h2("Main Panel"),
                                      textOutput("tab-name"))),
                          value = "Tab1"),
                 id = ns("active-tab"))
    }
    
    server <- function(input, output, session) {
      output[["tab-name"]] <- renderText({ input[["mainUI-active-tab"]] })
      output[["tab-name2"]] <- renderText({ input[["mainUI-active-tab"]] })
    
    }
    
    shinyApp(ui=ui("mainUI"),server=server)
    

    问题是输出 ID tab-name 在 UI 中出现了两次,这在 HTML 文档中无效,因此在 not valid in shiny 中无效。如果在模块中定义输出,事情当然会变得更加复杂。

    备注:我会避免 ID 包含像 tab-name 这样的减号,因为 - 已经用作模块的分隔符。此外,语法output$tab-name 会抛出类似object "name" not found 的错误。

    【讨论】:

    • 事实证明,我实际上试图只保留一个 textOutput,但仅在将 ns("tab-name") 保留在 headerPanel 中时,这是行不通的(不需要 ns)。非常感谢您的回答和额外的解释,它奏效了!
    猜你喜欢
    • 2019-03-03
    • 2020-12-11
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2021-07-31
    • 2016-11-29
    相关资源
    最近更新 更多