joe-gz joe-gz - 3 months ago 25
R Question

r shiny create a dropdown from a column of data in csv

My dropdown menu right now is populating with the column names. Instead, i want to create a dropdown that simply uses everything in the first column as the options. can anyone help with my output$toCol function?

server.R:

filedata <- reactive({
infile <- input$datafile
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
read.csv(infile$datapath, header = TRUE)
})

output$toCol <- renderUI({
df <-filedata()
if (is.null(df)) return(NULL)

items=names(df)
names(items)=items
selectInput("species-dropdown", "Species:",items)
})


ui.R

fileInput('datafile', 'Choose CSV file', accept=c('text/csv', 'text/comma-separated-values,text/plain')),
uiOutput("toCol")

Answer

First of all, instead of if (...) return(NULL) use req which will raise an a "silent" exception if input$datafile is NULL and it will prevent propagation of the errors. It will simplify your code and it is a very good practice to use it. See also validate and need.

The answer to your question is as simple as this:

    df <- filedata()
    # as.character: to get names of levels and not a numbers as choices in case of factors
    items <- as.character(df[[1]])
    selectInput("species-dropdown", "Species:", items)

I also added a small interface for uploading files.


Full example:

library(shiny)
rm(ui)
rm(server)

server <- function(input, output) { 

  filedata <- reactive({
    infile <- input$datafile
    # require that infile is not NULL (?req)
    # it will prevent propagating errors 
    req(infile) 

    # read.csv(infile$datapath, header = TRUE)
    iris
  })

  output$toCol <- renderUI({
    df <- filedata()

    # as.character: to get names of levels and not a numbers as choices in case of factors
    items <- as.character(df[[1]])

    selectInput("species-dropdown", "Species:", items)
  })

}

ui <- fluidPage(
  # http://shiny.rstudio.com/gallery/file-upload.html
  sidebarLayout(
    sidebarPanel(
      fileInput('datafile', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),
      tags$hr(),
      checkboxInput('header', 'Header', TRUE),
      radioButtons('sep', 'Separator',
                   c(Comma=',',
                     Semicolon=';',
                     Tab='\t'),
                   ','),
      radioButtons('quote', 'Quote',
                   c(None='',
                     'Double Quote'='"',
                     'Single Quote'="'"),
                   '"')
    ),
    mainPanel(
      uiOutput("toCol")
    )
  )
)

shinyApp(ui, server)