I've written a small shiny app to test the variable selection function for user uploaded data. Here is my code:
ui.R
shinyUI(pageWithSidebar(
headerPanel("CSV Data explorer"),
sidebarPanel(
fileInput('datafile', 'Choose CSV file',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
htmlOutput("varselect", inline=TRUE),
selectInput("vars", "Select a variable:",choices=htmlOutput("varselect")),
br()
),
mainPanel(
dataTableOutput("table")
)
))
server.R
shinyServer(function(session,input, output) {
Dataset <- reactive({
infile <- input$datafile
if (is.null(infile)) {
return(NULL)
}
read.csv(infile$datapath)
})
observe({
output$varselect <- renderUI({
if (identical(Dataset(), '') || identical(Dataset(),data.frame())) return(NULL)
updateSelectInput(session, inputId="vars", label="Variables to use:",
choices=names(Dataset()), selected=names(Dataset()))
})
})
output$table <- renderDataTable({
if (is.null(input$vars) || length(input$vars)==0) return(NULL)
return(Dataset()[,input$vars,drop=FALSE])
})
})
if you go ahead and test it on any of your csv files, you will see 2 problems: 1. there is a mess showing all the variable names above the selectInput() box and this is caused by the code:
htmlOutput("varselect", inline=TRUE)
but if I delete this line of code my selectInput is going to disappear.
- the selectInput only allows for single variable selection, if I change to
selectInput("vars", "Select a variable:",choices=htmlOutput("varselect"), multiple=TRUE),
and try to click on multiple choices, it is not going to be reflected in the table in the main panel.
I've been struggling with this problem for some time. Could someone help out please! Millions of thanks in advance!
Regards, mindy