R shiny: selectInput() doesn't reflect the variable selection when multiple=TRUE

1,707 views
Skip to first unread message

Mindy Fang

unread,
Oct 20, 2014, 4:00:50 AM10/20/14
to shiny-...@googlegroups.com

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.

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

phill...@gmail.com

unread,
Oct 23, 2014, 5:48:57 PM10/23/14
to shiny-...@googlegroups.com
Hi Mindy,

I've written an update (code below), which I think works as you intend. Changes are:

1. Add selectize = TRUE to selectInput
2. Remove output$varselect <- renderUI({ }) from server.R
3. Remove label="Variables to use:" from updateSelectInput
4. I also added an actionButton to ui.R and a new observer({}) to sever.R, to allow for the choices to be cleared from selectInput
5. Added h5 to labels to make prettier

I hope that helps! It's a nice little tool, and I might just have to use it on a project sometime :-D

# ui.R


shinyUI(pageWithSidebar(
  headerPanel("CSV Data explorer"),
  sidebarPanel(
   
    fileInput("datafile", h5("Choose CSV file:"),

              accept=c('text/csv', 'text/comma-separated-values,text/plain')),
   
    htmlOutput("varselect", inline=TRUE),
   
    selectInput("vars", label = h5("Select a variable:"), choices=htmlOutput("varselect"), selected = NULL, multiple=TRUE, selectize=TRUE),
   
    actionButton("unselect", label="Clear All"),

       
    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({

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

      updateSelectInput(session, inputId="vars",
                        choices=names(Dataset()), selected=NULL)
  })
 
  observe({
   
    if (input$unselect > 0) {

     
      if (identical(Dataset(), '') || identical(Dataset(),data.frame()))
        return(NULL)
     
      updateSelectInput(session, inputId="vars",
                        choices=names(Dataset()), selected=NULL)

    }
   
  })
 
  output$table <- renderDataTable({
   
    if (is.null(input$vars) || length(input$vars)==0) return(NULL)
   
    return(Dataset()[,input$vars,drop=FALSE])
  })
 
})

Mindy Fang

unread,
Oct 23, 2014, 6:41:53 PM10/23/14
to phill...@gmail.com, shiny-...@googlegroups.com
Hi Phill, Thanks for your time and your nice code! :-)

It is working perfectly now. But what puzzles me is that although there's htmlOutput(varselect) in ui.R, we have deleted the output$varselect from the server.R already.. 

It turned out in ui.R, the result of htmlOutput(varselect) is just NULL. So I removed all the htmlOutput(varselect) from ui.R and change the code in ui.R to
 selectInput("vars", label = h5("Select a variable:"), choice=NULL, selected = NULL, multiple=TRUE, selectize=TRUE),
  
with no change in server.R.  The code is working the same way as before. I had thought if we use both updateSelectOutput() in server.R and selectOutput() in ui.R for the same input variable, we would have to specify same choices for both of the functions. Seems like it's not necessary. 

Thanks Phill for your effort! I've learned a lot of new things from it. I can use the code in many of my projects as well.

Regards,
mindy

--
You received this message because you are subscribed to a topic in the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/shiny-discuss/QcgG9d8S-Hs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/8788bd94-89a3-44e6-b44e-7e3961210808%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages