I want to be able to update the selection available in the pickerInput based upon the selection made in the radio button input. In the example below, I want the radio button "A", to give a pickerInput list of mtcars$cyl, whilst a picker input of "B" to give a picker input of mtcars$mpg
I have tried doing this using if statements, but I haven't got anywhere so far. Reproducible example below:
library(shiny)
library(leaflet)
library(shinyjs)
library(rgeos)
library(tidyverse)
library(openxlsx)
library(plotly)
library(shinyWidgets)
library(rgdal)
ui <- fluidPage(shinyjs::useShinyjs(),
fluidRow(column(
6,radioButtons("type", "Type:",
c("A" = "norm",
"B" = "unif")))),
fluidRow(column(
3,
pickerInput("regInput","Region",choices=unique(mtcars$cyl),
options = list(`actions-box` = TRUE),multiple = T,
selected = unique(mtcars$cyl)))))
server <- function (input, output, session) {
observeEvent(input$type,{
a_option <- input$regInput
if (a_option == "B") {
updatePickerInput(session = session,inputId = "regInput",
choices =unique(mtcars$mpg))}})
}
shinyApp(ui = ui, server = server)
As said in the other answer you need to observe
input$type
and update the picker input according to the values in it. Also, it's important to include an else statement, to be able to update the picker input when the user picks between the radio button choices multiple times. Finally, on theupdatePickerInput
you need to include the selected argument to maintain the behaviour as it is on the ui.Here's the code doing what I've described above: