3
votes

I am new to shiny and having hard time to figure this out. I am trying to create a "Select ALL" button in my selectizeInput but I am getting an error while passing the input from selectizeInput to the updateSelectizeInput. Could someone please help me to resolve this.

When I select "Select ALL" from the input box, the App closes and shows the error: "$ operator is invalid for atomic vectors"

I added "Select All" in the input field (selectizeInput()). When an user clicks "Select All", updateSelectizeInput() adds the set difference between all names in the input and "Select All", populating the filter box with all values.

Data for the script: https://drive.google.com/file/d/0B_sB3ef_9RntOWJjNlhrUjk3a1k/view

Here is my Script

UI.R

library(shiny)
shinyUI(fluidPage(
  navbarPage(
    "Tab_name",
    tabPanel("Engine",
             bootstrapPage(
               div(style="display:inline-block", fileInput("file_attr", "attributes:")),
               uiOutput("CountryList")
             )         
    )
  )
))

Server.R

library(shiny)
shinyServer(function(input, output) {     
  data_attr <- reactive({
      file1 <- input$file_attr
      if(is.null(file1)){return()} 
      read.table(file=file1$datapath, sep=",", header = TRUE, stringsAsFactors = FALSE)       
    })

    countries <- reactive({
      if(is.null(data_attr()$Country)){return()}
      data_attr()$Country
    })  

    observeEvent(input$file_attr, {
      output$CountryList <- renderUI({
        if(is.null(data_attr()$Country)){return()}
        selectizeInput('show_vars', 'Country Filter', choices = c("Select All",unique(countries())), multiple = TRUE)
      })
    }) 

    observe({
      if ("Select All" %in% input$show_vars){
        selected_choices <- setdiff(c("Select All",unique(countries())), "Select All")
        updateSelectizeInput('show_vars', 'Country Filter', selected = selected_choices)

      }

    })

  })

Error comes when we select "select all" from the input field of the UI

Warning: Error in $: $ operator is invalid for atomic vectors
Stack trace (innermost first):
    58: updateSelectInput
    57: updateSelectizeInput
    56: observerFunc [C:\Users\naresh.ambati\Documents\dummt/server.R#30]
     1: runApp
ERROR: [on_request_read] connection reset by peer

Thanks!

1

1 Answers

5
votes

The error is because you have got the arguments to be passed to the update updateSelectizeInput is incorrect. You need to pass the session object as the first argument. In your case it should be something like this:

updateSelectizeInput(session,'show_vars', 'Country Filter', selected = selected_choices)

And while you define the server function you need to pass session object as follows:

shinyServer(function(input, output, session) { 

.......

} 

Hope it helps!