1
votes

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.

  1. 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

1
So to clarify, are you just trying to have the user select which variables (i.e. column names) to display in the datatable? What is your goal?cdeterman
Hi cdeterman, thanks for your response. Yes I try to allow user to select multiple variables to display in the table (as well as for further analysis such as multivariate regression, which is not yet reflected in the above codes).cloudy

1 Answers

0
votes

I think you have made yours overly complicated. Briefly, you only really need a uiOutput in place of your htmlOutput and selectInput statements in your ui.R. There also doesn't appear to be any need for the updateSelectInput or observe functions. You can simplify your code to the following and the multiple select appears to work appropriately:

ui.R

shinyUI(pageWithSidebar(
  headerPanel("CSV Data explorer"),
  sidebarPanel(

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

    uiOutput("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)
  })

  output$varselect <- renderUI({

    if (identical(Dataset(), '') || identical(Dataset(),data.frame())) return(NULL)

    cols <- names(Dataset())
    selectInput("vars", "Select a variable:",choices=cols, selected=cols, multiple=T)  

  })

  output$table <- renderDataTable({

    if (is.null(input$vars) || length(input$vars)==0) return(NULL)

    return(head(Dataset()[,input$vars,drop=FALSE]))
  })

})