Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force no default selection in selectInput()

Tags:

r

shiny

The Shiny documentation mentions that for selectInput():

selected The value (or, if none was supplied, the title) of the navigation item that should be selected by default. If NULL, the first navigation will be selected.

What if by default I don't want to select any value from select list?

Actually my select value is getting selected by default and rest part of app is getting executed automatically. But I don't want to select any value initially. What should I supply to the selected argument in selectInput() to do that?


Indeed, I don't want anything to be selected automatically. I used the code below but still it's selecting first available value from the list. I want there to be no selection by default, so the user can then select any option.

output$Choose_App <- renderUI({
selectInput("app",
            "Select App:",
            choices = as.character(mtrl_name),
            selected = NULL ,
            multiple = FALSE
           )
        })

Going through the documentation I noticed that the selection can be empty only if I select multiple=TRUE. Is this correct?

When I changed to multiple=TRUE, then it it's not getting selected by default, which is what I want. But unfortunately before making any selection I am also getting following error message:

ERROR: bad 'file' argument 

Does anybody know about this if I am doing something wrong? But if I select this file then error is gone.

enter image description here

I am using following code for this:

# server.R
setwd("/opt/shiny-server/samples/sample-apps/P-Dict_RDS2")
mtrl_name <- try(system("ls | grep -i .rds", intern = TRUE))

shinyServer(function(input, output) {

# First UI input (Service column) filter clientData 
output$Choose_Molecule <- renderUI({
selectInput("molecule",
            "Select Molecule:",
            choices = as.character(mtrl_name),
            selected = input$molecule,
            multiple = TRUE
           )
        })
like image 972
Piyush Avatar asked Jun 12 '14 03:06

Piyush


3 Answers

You can use the selectize input instead of the select input, with some custom selectize options to set the initial selection to be empty. An example has been provided in the Shiny Gallery. In particular, see the example #6.

# make sure you have shiny >= 0.9.1
selectizeInput(
  'e6', '6. Placeholder', choices = state.name,
  options = list(
    placeholder = 'Please select an option below',
    onInitialize = I('function() { this.setValue(""); }')
  )
)

BTW, for the error "ERROR: bad 'file' argument", I do not think anybody can help you without seeing the source code of your app, and it may be a separate question.

like image 117
Yihui Xie Avatar answered Nov 06 '22 22:11

Yihui Xie


Faced a similar issue. The solution I found is based of @MKa's answer. You do not want to set multiple=T if your code can't handle multiple values. What I suggest is:

selectInput("molecule",
            "Select Molecule:",
            choices = c("",as.character(mtrl_name)),
            selected = NULL,
            multiple = F
           ) 

And to retrieve the value selected:

if(!is.null(input$molecule))
{
  if(nchar(input$molecule)>1)
  {
    #do your thing..
  }
}

Fixed my problem. Let me know if you found a better solution.

like image 28
rookieJoe Avatar answered Nov 06 '22 22:11

rookieJoe


I think you can get around it by adding an empty string to your choice list:

selectInput("app", 
"Select App:", 
choices = c("", as.character(mtrl_name)), 
selected = NULL, 
multiple = FALSE)
like image 18
MKa Avatar answered Nov 06 '22 22:11

MKa