简体   繁体   中英

How do I use tagList() in a Shiny module?

The official tutorial by RStudio is a little unclear on how to actually use the tagList() function for creating a namespaced ID in Shiny modules. The Shiny documentation does not help much either. What exactly am I supposed to put inside the tagList() function? Should I only wrap input arguments in a tagList(), like shown in all examples and video tutorials, or can I put other elements in there, such as a sidebarPanel()?

tagList is nothing but creating a list of tags. The definition is

> tagList
function (...) 
{
    lst <- list(...)
    class(lst) <- c("shiny.tag.list", "list")
    return(lst)
}

It's a simple list having a special class shiny.tag.list . You use it when you create a module, where the UI of the module needs to return a simple page, like a fluidPage etc. If you don't want to create an extra UI for the module, you just return a few UI elements wrapped inside tagList and take care of the UI outside of the module. For example:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM