Anshu
Anshu

Reputation: 21

How to add space/blank option in selectInput in R

I am trying to use selectInput function and in drop down wanted to add space/ blank option for user to select. example:

shinyApp(
  ui = fluidPage(
    selectInput('selalpha', "Select Alpha", choices=c("", "a", "b", "c"), selected ="a")
  ),
  server = function(input, output, session) {
    
  }
)

In Choice parameter of this function, i am passing a vector such as choice = c("", "a","b","c"). but while running i am only seeing a,b,c option in dropdown.

The dropdown needs to have a default value of a but the user can have option to change that to spaces from dropdown.

Upvotes: 1

Views: 2700

Answers (3)

LMc
LMc

Reputation: 18642

You could use pickerInput from the package shinyWidgets with minimal change to your code:

library(shiny)
library(shinyWidgets)

shinyApp(
  ui = fluidPage(
    # pickerInput instead of selectInput
    pickerInput('selalpha', "Select Alpha", choices=c("", "a", "b", "c"), selected ="a")
  ),
  server = function(input, output, session) {
    
  }
)

pickerInput also has a lot of customization options.

Upvotes: 0

msr_003
msr_003

Reputation: 1233

A small trick will do this.

shinyApp(
  ui = fluidPage(
    selectInput('selalpha', "Select Alpha", choices = c("\a", "a", "b", "c"), selected = "a")
  ),
  server = function(input, output, session) {
  }
)

This gives you a proper empty space which will allow you to select. Another option : use \n instead of \a. In this case it is a little hard to check in selectInput output but a blank will come for sure. So to confirm this use choices = c("\nz", "a", "b", "c") then you could see just z in the dropdown choices.

Upvotes: 2

Pavan Sista
Pavan Sista

Reputation: 92

Rather than using ""(quotes without spaces), " "(quotes with a space between them).

Upvotes: 1

Related Questions