'Updating pickerInput based on radiobutton

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)


Solution 1:[1]

The server has to listen to the right UI Element (ID = "type" for the radio buttons in question). Currently it observes an undefined element "dist".

Try changing

observeEvent(input$dist, { code })

to

observeEvent(input$type, { code })

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