Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

shiny

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()?

like image 661
Samuel Avatar asked Dec 20 '16 11:12

Samuel


Video Answer


1 Answers

tagList() creates nothing but a simple 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)
like image 169
shosaco Avatar answered Oct 22 '22 15:10

shosaco