Try the code below, explained inline. If anything is unclear, let me know and I will be happy to elaborate.
library(shiny)
series_choices = c("A class", "B class")
fund_choices = c("Fund A", "Fund B", "Fund C", "Fund D")
fund_series = c("A class", "A class", "B class", "B class")
# Replace your fund_* variables with a data.frame, since you
# want to filter one vector based on another. Not exactly necessary,
# but will keep things a bit cleaner if you add other fund-related
# information.
funds <-
data.frame(
choices = c("Fund A", "Fund B", "Fund C", "Fund D"),
series = c("A class", "A class", "B class", "B class"),
stringsAsFactors = FALSE
)
# Shiny app ----
ui <- fluidPage(titlePanel("Fund Comparison"),
sidebarLayout(# Sidebar panel for inputs ----
sidebarPanel(
# Series selector
radioButtons(
inputId = "series_selector",
label = "Series:",
choices = series_choices,
inline = TRUE,
selected = "A class"
),
# Fund selector
selectizeInput(
inputId = "funds_selector",
label = "Funds:",
# We can initialize this as NULL because
# we will update in the server function.
choices = NULL,
multiple = TRUE
),
),
# Main panel for displaying outputs ----
mainPanel()))# [Some charts coming later]))))))
# I've added the optional "session" variable to the server function.
# this is necessary for Shiny applications which will have different behavior
# based upon user inputs. In this case, the different behavior is changing
# the selectizeInput choices.
server <- function(input, output, session) {
# Whatever is inside `observeEvent()` will be triggered each time the first
# argument undergoes a change in value. In this case, that means whenever
# the user changes the radio button value.
observeEvent(input$series_selector,
{
# Use this function to update the choices for the user.
# First argument is session, next the input to update,
# and third the new choices. Here, I'm filtering the
# previously made data.frame to based on the series column,
# and returning the choices column.
# `drop=TRUE` makes it explicit that I want a vector returned.
updateSelectizeInput(session, input = "funds_selector",
choices = funds[funds$series %in% input$series_selector,
"choices", drop = TRUE])
})
}
# Create Shiny app ----
shinyApp(ui, server)