'Selectize input with the initial choice after the click of a button
I have a number of selectize inputs (some single choice and some multiple choice). Here is a snippet of my code:
# ui.R
library(shiny)
shinyUI(fluidPage(
selectizeInput(
"countries",
"",
choices = c("Greece", "Italy", "France", "Belgium", "Latvia"),
multiple = FALSE
),
selectizeInput(
"resources",
"",
choices = c("gold", "silver", "gas", "oil", "wheat"),
multiple = TRUE
),
actionButton(
"reset",
"Reset"
)
))
From what I understand, the default choice for the single-choice menu should be "", while for the multiple-choice menu it should be NULL.
I would like that when I click a reset button the choices of the selectize inputs go back to the initial ones, that is when I start the app. Is there any way to do this? Below is my try but it doesn't work for the multiple choice menu, and in any case I hope there is a better and more efficient option.
# server.R
library(shiny)
shinyServer(function(input, output, session) {
observeEvent(input$reset, {
updateSelectizeInput(session, "countries", selected = "")
updateSelectizeInput(session, "resources", selected = NULL)
})
})
Anyone know a practical solution?
Solution 1:[1]
You didn't define your label and your selected default. It's a good idea to do that. In the Docs it states selectize will use "" as a default if you put it in the choices instead of defining it in selected.
library(shiny)
ui <- fluidPage(
selectizeInput(
"countries",
label = "Countries",
choices = c("", "Greece", "Italy", "France", "Belgium", "Latvia"),
multiple = FALSE
),
selectizeInput(
"resources",
label = "Resources",
choices = c("","gold", "silver", "gas", "oil", "wheat"),
multiple = TRUE
),
actionButton(
inputId = "reset",
label = "Reset"
)
)
server <- function(input, output, session) {
observeEvent(input$reset, {
purrr::map(c("countries", "resources"),~ updateSelectizeInput(inputId = .x, selected = ""))
})
}
shinyApp(ui, server)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jahi Zamy |
