Set selecInput to have no option selected

When running the app, you will see that Option 1 is already selected, however it is strange because in ui I inserted selected="No option selected". So what am I doing wrong?

My idea is that when running the algorithm, there is no option selected in selectInput.

Executable code below:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fluidRow(
        column(
          width = 6,
          selectInput("test", label = h5("Choose"),choices = list("Option1 " = "1", "Option2" = "2", selected="No option selected")),
      ))),
    mainPanel(

    )
  )
)

server <- function(input, output, session) {
  
}

shinyApp(ui = ui, server = server)

enter image description here

          selectInput("test",
            label = h5("Choose"),
            choices = list(
              "No option selected" ="",
              "Option1 " = "1",
              "Option2" = "2"
            ))
1 Like

Thanks for the answer @nirgrahamuk . Do you know how I would be able to insert this option No option selected in a numericInput, for example:

numericInput("num", label = h5("Numeric input"), min = 0, max = 1, value = NA, step = 0.1)

"No option selected" is not a number so I would be surprised that this is possible.

shiny numericInput does not support this at present. but it would be an easy extension, it might be done like this. I may offer this to the shiny project as a small feature enhancement, because it seems potentially useful.

library(shiny)

mynumericInput <- function (inputId, label, value, min = NA, max = NA, step = NA, 
          width = NULL,placeholder=NA_character_) 
{
  value <- shiny:::restoreInput(id = inputId, default = value)
  inputTag <- tags$input(id = inputId, type = "number", 
                         class = "form-control", value = shiny:::formatNoSci(value))
  if (!is.na(min)) 
    inputTag$attribs$min = min
  if (!is.na(max)) 
    inputTag$attribs$max = max
  if (!is.na(step)) 
    inputTag$attribs$step = step
  if (!is.na(placeholder)) 
    inputTag$attribs$placeholder = placeholder
  div(class = "form-group shiny-input-container", style = htmltools:::css(width = htmltools:::validateCssUnit(width)), 
      shiny:::shinyInputLabel(inputId, label), inputTag)
}


ui <- fluidPage(
  numericInput("num1", label = h5("Numeric input 1"), min = 0, max = 1, value = NA, step = 0.1),
  mynumericInput("num2", label = h5("Numeric input 2"), min = 0, max = 1, value = NA, step = 0.1,placeholder = "test")
  
)

server <- function(input, output, session) {
  
}

shinyApp(ui, server)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.