Shiny - use column headers from read in file as selectInput choices

a 夏天 提交于 2021-02-20 04:06:25

问题


I'm trying to create an app where people can upload a CSV and then interact with the data. The specific issues is I'm failing to pass the column headers from the file that is read in to the selectInput function.

If you comment out the last few lines for the observe function the app works fine. Have tried a number of options, using reactive instead of renderTable etc. There are some similar questions around addressing changing the select input but none that I could see from a file read in. Seems the lack of anything in 'contents' to begin with is the issue?

require(shiny)
runApp(
list(
ui = fluidPage(
  sidebarPanel(fileInput('file1', 'Choose CSV File',
                         accept=c('text/csv', 
                                  'text/comma-separated-values,text/plain', 
                                  '.csv')),
               selectInput("inSelect", "Select something", choices = "Pending Upload")
  ),
  mainPanel(
    tableOutput("contents"))
),

server = function(input, output) {
    output$contents <- renderTable({
    inFile <- input$file1
    if (is.null(inFile))
      return(NULL)
    read.csv(inFile$datapath)
  })

  observe({
    updateSelectInput(session, "inSelect", choices = names(contents()))
  })

}
)

Test CSV

Col1,Col2,Col3
2,3,4
5,6,7

回答1:


You have a couple of issues with your code. In order of updateSelectInput to work, you need to pass the session object to your shinyServer function.

Then, you have to create a reactive context that reads the input file. This is needed since the result of this operation is used both to create the output table and to change the values of the selectInput.

This should work:

server = function(input, output, session) {
  contentsrea <- reactive({
     inFile <- input$file1
     if (is.null(inFile))
        return(NULL)
     read.csv(inFile$datapath)
  })
  output$contents<-renderTable({contentsrea()})
  observe({
     updateSelectInput(session, "inSelect", choices = names(contentsrea()))
  })
} 


来源:https://stackoverflow.com/questions/30906325/shiny-use-column-headers-from-read-in-file-as-selectinput-choices

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!