Listing choices for selectInput()
is done by hardcoding the values, as in the example from ?selectInput
:
selectInput(inputID = "variable", label ="variable:",
choices = c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"))
However, I'd like my list of choices
to be a unique listing of values from a user-selected column coming from a file (csv) uploaded by the user. How might I do this? This is as far as I've gotten:
UI
shinyUI(fluidPage(
fluidRow(
fileInput('datafile', 'Choose CSV file',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
uiOutput("selectcol10"),
uiOutput("pic")
))
)
SERVER
shinyServer(function(input, output) {
filedata <- reactive({
infile <- input$datafile
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
temp<-read.csv(infile$datapath)
#return
temp[order(temp[, 1]),]
})
output$selectcol10 <- renderUI({
df <-filedata()
if (is.null(df)) return(NULL)
items=names(df)
names(items)=items
selectInput("selectcol10", "Primary C",items)
})
col10 <- reactive({
(unique(filedata()$selectcol10))
})
output$pic <- renderUI({
selectInput("pic", "Primary C values",col10())
})
})
For dynamic UIs there are basically two routes you can take:
updateXXX
orrenderUI
. Here is a solution that takes theupdateXXX
approach.In the
server
function, there is the linewhich updates the
choices
argument of the third dropdown menulevel
. To calculate levels, you can use thebase::levels
function, but that does not work for numerc columns. Therefore I usedinstead. The "placeholders" will be replaced immediately after startup.
dataframes created with fileInput
The code below shows how this logic can be combined with
fileInput
. I added aconitionalPanel
in theui
to hide the dropdown-menus as long as no file is selected. See here.This is what worked, with credit to @GregordeCillia for leading me to it:
in SERVER I replaced
with
and in the UI I replaced
with