I am trying to update the choices in a selectizeInput()
based on whether the user clicks "Common name" or "Scientific name" buttons. The default is "Common name".
I am aware of conditionalPanel()
from this answer, but my choices will link to an output plot so I need them to be reactive. Therefore, upon clicking "Scientific name", I want the current choices to be cleared and then only the new choices (names_vector2) are available to be selected. Similarly, if the user then clicks back to "Common name" I want the current choices cleared and only choices from names_vector1 to be available for selection.
Hope that makes sense!
library(shiny)
library(shinyWidgets)
names_vector1 = paste0("common", 1:10)
names_vector2 = paste0("scientific", 1:10)
ui = fluidPage(
fluidRow(
selectizeInput(
inputId = "species_selector",
label = "Choose a species:",
selected = "common1",
choices = c("Choose" = "", names_vector1),
options = list(
maxOptions = 5,
maxItems = 4
)
),
awesomeRadio(
inputId = "species_selector_type",
label = NULL,
choices = c("Common name","Scientific name"),
selected = "Common name",
inline = TRUE
)
)
)
server = server = server = function(input, output, session){
# I want to change the selectizeInput choices as the user clicks the buttons:
# "Common name" and "Scientific name"
observeEvent(input$species_selector_type {
if (input$species_selector_type == "Scientific name")
updateSelectizeInput(
session,
inputId = "species_selection",
choices = c("Choose" = "", names_vectors),
)
})
# The desired result is to:
# 1. Clear the current selectiveInput selected names each time a new button is clicked
# 2. Update the choices so that:
# Common name = names_vector1
# Scientific name = names_vector2
}
shinyApp(ui, server)
CodePudding user response:
You were almost there - added an else if statement:
library(shiny)
library(shinyWidgets)
names_vector1 = paste0("common", 1:10)
names_vector2 = paste0("scientific", 1:10)
ui = fluidPage(fluidRow(
selectizeInput(
inputId = "species_selector",
label = "Choose a species:",
selected = "common1",
choices = c("Choose" = "", names_vector1),
options = list(maxOptions = 5,
maxItems = 4)
),
awesomeRadio(
inputId = "species_selector_type",
label = NULL,
choices = c("Common name", "Scientific name"),
selected = "Common name",
inline = TRUE
)
))
server = server = server = function(input, output, session) {
observeEvent(input$species_selector_type, {
if (input$species_selector_type == "Common name") {
updateSelectizeInput(session,
inputId = "species_selector",
choices = c("Choose" = "", names_vector1))
} else if (input$species_selector_type == "Scientific name") {
updateSelectizeInput(session,
inputId = "species_selector",
choices = c("Choose" = "", names_vector2))
}
})
}
shinyApp(ui, server)