使用上传文件中的数据创建多个闪亮的小部件

create multiple shiny widgets with data from uploaded file

shiny 中,如何使用 renderUI 中的 tagList 来创建多个使用上传文件中的数据自定义的小部件?这个想法被引用 here,但是 tagList.

似乎没有很好的文档

我打算在这里回答我自己的问题。我做了一些研究,发现缺少此过程的简单示例,并希望贡献它以便其他人受益。

在server.R中,使用reactive()语句定义一个对象来保存上传文件的内容。然后,在 renderUI 语句中,将以逗号分隔的小部件定义列表包装在 tagList 函数中。在每个小部件中,使用保存上传文件内容的对象作为小部件参数。下面的示例 hosted at shinyapps.io and available on github 使用基于上传文件定义的 singer renderUI 创建了一个 checkBoxGroupInput 和一个 radioButtons 小部件。

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")
    )
  )
))