Changing values of selectInput conditionally

Hi there! I want to do the following:

  1. Have a selectInput field whose values change depending on a radiobutton.

  2. Do something with the value selected in the selectInput

In the code below 1. seems to be working, but 2. only works for the "high" option (or whatever is the first conditionalPanel selectInput I have in my code).

library(shiny)
library(tidyverse)

ui <- fluidPage(
  
  mainPanel(
      radioButtons("thresholds", "Thresholds",
                   c("high", "low"), selected = "high"),
      
      conditionalPanel(
        condition = "input.thresholds == 'high'",
        selectInput('test', 'test', 
                    cars %>% filter(speed >= 14) %>% pull(speed), 
                    multiple = TRUE, selectize = FALSE, width = 300, size = 20)),
      
      conditionalPanel(
        condition = "input.thresholds == 'low'",
        selectInput('test', 'test', 
                    cars %>% filter(speed < 14) %>% pull(speed), 
                    multiple = TRUE, selectize = FALSE, width = 300, size = 20)),
    
    verbatimTextOutput('out1')))


server <- function(input, output) {
  
  output$out1 <- renderPrint(input$test)
}

shinyApp(ui = ui, server = server)

I hope my explanation is clear enough... and thanks for the help!

I would give different names to the two selectInput() objects and use an if() in the renderPrint() to print the desired one.

library(shiny)
library(tidyverse)

ui <- fluidPage(
  
  mainPanel(
    radioButtons("thresholds", "Thresholds",
                 c("high", "low"), selected = "high"),
    
    conditionalPanel(
      condition = "input.thresholds == 'high'",
      selectInput('test', 'test', 
                  cars %>% filter(speed >= 14) %>% pull(speed), 
                  multiple = TRUE, selectize = FALSE, width = 300, size = 20)),
    
    conditionalPanel(
      condition = "input.thresholds == 'low'",
      selectInput('test2', 'test', 
                  cars %>% filter(speed < 14) %>% pull(speed), 
                  multiple = TRUE, selectize = FALSE, width = 300, size = 20)),
    
    verbatimTextOutput('out1')))


server <- function(input, output) {
  
  output$out1 <- renderPrint(if (input$thresholds == "high") input$test else input$test2)
}

shinyApp(ui = ui, server = server)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.