Home > OS >  updateSelectInput fails to clear input object
updateSelectInput fails to clear input object

Time:06-24

In an R Shiny app, when trying to blank-out the choices and selection of a select object using the updateSelectInput() function, the input object does not reset. The input object retains the last choice selected.

The choice and selection are indeed removed from the select dropdown as I set them to character(0) (per references), but the input object resists reseting. Here is a very hackish solution I'm trying to avoid.

Is there a way to reset an input object to NULL, or character(0)? I know input is read only, but I'm wondering if I can reset it when the selectInput has been reset.

library(shiny)

cs <- c("A","B")

ui <- basicPage(
  selectInput("options","Select", choices=NULL, selected = NULL),
  actionButton("add","Add options"),
  actionButton("clear","Clear options"),
  verbatimTextOutput("text")
)

server <- function(input, output, session) {
  
  observeEvent(input$clear, {
    updateSelectInput(session,"options",
                      choices = character(0), 
                      selected = character(0))
  })
  
  observeEvent(input$add, {
    updateSelectInput(session,"options",
                      choices = c("A","B"), 
                      selected = NULL)
  })
  
  output$text <- renderPrint({
    str(input$options)
  })
}

shinyApp(ui, server)

CodePudding user response:

Setting the argument selectize to FALSE can produce a NULL.

ui <- basicPage(
  selectInput("options",
   "Select",
   choices = NULL,
   selected = NULL,
   selectize = FALSE
  ),
  actionButton("add","Add options"),
  actionButton("clear","Clear options"),
  verbatimTextOutput("text")
)

CodePudding user response:

Its to do with internal implementation of the updateSelectInput as per Resetting selectInput to NULL in R Shiny, not ideal but try adding an extra space to "clear it"

  observeEvent(input$clear, {
    updateSelectInput(session,"options",choices = " ", selected = " ")
  })
  • Related