Paul de Barros
Paul de Barros

Reputation: 1200

create multiple shiny widgets with data from uploaded file

In shiny, how do you use tagList inside renderUI to create multiple widgets customized with data from an uploaded file? This idea is referenced here, but there doesn't appear to be very good documentation for tagList.

I plan to answer my own question here. I did a bit of research, found that a simple example of this process was lacking, and felt a desire to contribute it so that others might benefit.

Upvotes: 2

Views: 813

Answers (1)

Paul de Barros
Paul de Barros

Reputation: 1200

In server.R, define an object using a reactive() statement to hold the contents of the uploaded file. Then, in a renderUI statement, wrap a comma-delimited list of widget definitions in a tagList function. In each widget, use the object holding the contents of the uploaded file for the widget parameters. The example below, hosted at shinyapps.io and available on github, creates a checkBoxGroupInput and a radioButtons widget using a singer renderUI that is defined based on an uploaded file.

server.R

library(shiny)
shinyServer(function(input, output) {  

  ItemList = reactive(
    if(is.null(input$CheckListFile)){return()
    } else {d2 = read.csv(input$CheckListFile$datapath)
            return(as.character(d2[,1]))}
  )

  output$CustomCheckList <- renderUI({
    if(is.null(ItemList())){return ()
    } else tagList(
      checkboxGroupInput(inputId = "SelectItems", 
                         label = "Which items would you like to select?", 
                         choices = ItemList()),
      radioButtons("RadioItems", 
                   label = "Pick One",
                   choices = ItemList(), 
                   selected = 1)
    )
  })
})

ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("Create a checkboxGroupInput and a RadioButtons widget from a CSV"),
  sidebarLayout(
    sidebarPanel(fileInput(inputId = "CheckListFile", label = "Upload list of options")),
    mainPanel(uiOutput("CustomCheckList")
    )
  )
))

Upvotes: 3

Related Questions