Is it possible to pass the output value from the server portion of a shiny app into a selectInput portion of a conditionalPanel? I'm trying the following, but it doesn't seem to be using the output value from output.items_list_selection in the switch statement to render what I'm after.
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
# the user can choose from two options within the radio buttons
radioButtons("items_list_selection", label = "select item list"
, choices = list("Items - Item number & name" = "Both", "Items - Item name only" = "Name"), selected = "Both")
, conditionalPanel(
condition = "output.items_list_selection"
, selectInput("items", label = 'Select items'
# this is where I don't think I'm properly passing the value from output
, choices = switch( "output.items_list_selection", "Both" = items_names_id, "Name" = items_names)
, multiple = TRUE))
# action button so the user can submit for analysis based on their selected options
, actionButton("go", "Run", style = "color: white; background-color: #2759aa")
)
)
server <- function(input, output){
output$items_list_selection <- reactive({
input$items_list_selection
})
outputOptions(output, "items_list_selection", suspendWhenHidden = FALSE)
}
Your description was a bit confusing, but if it's the selectInput you'd like to update on the go, you can use the updateSelectInput function on the server side.
This is what I assumed your goal was:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
# the user can choose from two options within the radio buttons
radioButtons("items_list_selection", label = "select item list"
, choices = list("Items - Item number & name" = "Both", "Items - Item name only" = "Name"), selected = "Both")
, selectInput("items", "Select items", choices = "")
# action button so the user can submit for analysis based on their selected options
, actionButton("go", "Run", style = "color: white; background-color: #2759aa")
),
mainPanel()
)
)
server <- function(input, output, session){ #add session variable if updating UI
observeEvent(input$items_list_selection, {
#Here add code to filter options based on items_list_selection
myChoices = paste(input$items_list_selection, "_option_", 1:5, sep = "")
#Update the selectInput
updateSelectInput(session, "items", choices = myChoices)
})
observeEvent(input$go, {
#Button code goes here...
})
}
shinyApp(ui, server)
Thank you very much for the reply and my apologies for the confusing explanation/description.
Your suggestion was something I had actually attempted at implementing beforehand. While it did exactly what I wanted, it did impede performance of the app quite a bit (most notably when switching between lists a user may selected in my Shiny app), hence I continued to explore other potential solutions.
What I was trying to explain in the original post is that conditionalPanel allows for certain UI rendering depending on whether a certain condition is met. In my original post, that condition was condition = "output.items_list_selection" which could take one of the two options "Both" or "Name". It seems that R/Shiny is able to use either of these two values and extract it from the "output.items_list_selection" string at this place in the code, however, just a few lines down in the switch statement, R/Shiny doesn't interepret "output.items_list_selection" as either one of "Both" or "Name", but literally as a string.
I imagined it would behave as it would when evaluated at the condition = level of the code, but it doesn't. If there was somehow I could perhaps extract the value held within "output.items_list_selection" to pass to the switch statement, that would be ideal. Hope this is a little more clear.
I'm sorry it didn't work out as you liked. I think your approach is a bit convoluted as the conditional panel is made for something else than feeding into a switch function (which is an R function and is not going to work in the UI).
The only way I see it is creating two conditional panels, each with their own input, and displaying it depending on the choice of items_list_selection. Still,you'll need the updateSelectInput if you want to update the choices of the lists themselves on the go...
Give this a try:
library("shiny")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
# the user can choose from two options within the radio buttons
radioButtons("items_list_selection", label = "select item list"
, choices = list("Items - Item number & name" = "Both", "Items - Item name only" = "Name"), selected = "Both")
, conditionalPanel(
condition = "output.items_list_selection == 'Both'"
, selectInput("itemsBoth", label = 'Select items'
# this is where I don't think I'm properly passing the value from output
, choices = c("Both_Choice1", "Both_Choice2", "Both_Choice3")
, multiple = TRUE)),
conditionalPanel(
condition = "output.items_list_selection == 'Name'"
, selectInput("itemsName", label = 'Select items'
# this is where I don't think I'm properly passing the value from output
, choices = c("Name_Choice1", "Name_Choice2")
, multiple = TRUE))
# action button so the user can submit for analysis based on their selected options
, actionButton("go", "Run", style = "color: white; background-color: #2759aa")
),
mainPanel()
)
)
server <- function(input, output){
output$items_list_selection <- reactive({
input$items_list_selection
})
outputOptions(output, "items_list_selection", suspendWhenHidden = FALSE)
}
shinyApp(ui, server)
It has the same result, and I'd be surprised if it were faster than my first suggestion, but since I still don't get your issue 100% it might help ...