Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updateSelectInput fails to clear input object

Tags:

r

shiny

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)
like image 761
guasi Avatar asked Oct 28 '25 14:10

guasi


1 Answers

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")
)
like image 152
jpdugo17 Avatar answered Oct 31 '25 05:10

jpdugo17



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!