如何在 Shiny 模块中使用 tagList()?

jsb*_*jsb 7 r shiny

RStudio的官方教程对于如何实际使用 tagList() 函数在 Shiny 模块中创建命名空间 ID 有点不清楚。在闪亮的文档没有太大的帮助要么。我到底应该在 tagList() 函数中放什么?我应该只将输入参数包装在 tagList() 中,如所有示例和视频教程中所示,还是可以将其他元素放入其中,例如 sidebarPanel()?

sho*_*aco 8

tagList()只创建一个简单的标签列表。定义是

> tagList
function (...) 
{
    lst <- list(...)
    class(lst) <- c("shiny.tag.list", "list")
    return(lst)
}
Run Code Online (Sandbox Code Playgroud)

这是一个具有特殊类的简单列表shiny.tag.list。你在创建模块时使用它,模块的UI需要返回一个简单的页面,比如afluidPage等。如果你不想为模块创建额外的UI,你只需返回一些包裹在里面的UI元素tagList并处理模块外部的 UI。例如:

library(shiny)

moduleUI <- function(id){
    ns <- NS(id)
    
    # return a list of tags
    tagList(h4("Select something"),
            selectInput(ns("select"), "Select here", choices=1:10))
  }

module <- function(input, output, session){}
  
ui <- shinyUI(
  fluidPage(wellPanel(moduleUI("module"))) # wrap everything that comes from the moduleUI in a wellPanel
)

server <- shinyServer(function(input, output, session){
  callModule(module, "module")
})

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