2

This is an extension of the question asked here: R shiny Multiple slider inputs based on checkbox inputs. The issue I have is that my checkbox "One" should open sliders 1 and 2 and checkbox "Three" should open sliders 1 and 3. If I check "One" and "Three" I am getting slider 1 twice :(. Similarly, checking "One" and "Two" opening slider 2 twice :(. Expected behavior would be that only one slider is being used. My apologies in advance if this is trivial!

library(shiny)
myData = c("One", "Two", "Three")

ui <- fluidPage(
  
  checkboxGroupInput("choosemedia", "Choose digital", 
                     choices  = myData,
                     selected = myData),
  textOutput("myText"),
  conditionalPanel(condition = "input.choosemedia.includes('One')", 
                   sliderInput("sliderOne", "Choose value no 1", 
                               min=0, max=100, value=50),
                   sliderInput("sliderTwo", "Choose value no 2",
                               min=0, max=50, value=25)),
  
  conditionalPanel(condition = "input.choosemedia.includes('Two')",
                   sliderInput("sliderTwo", "Choose value no 2", 
                               min=0, max=50, value=25)),
  
  conditionalPanel(condition = "input.choosemedia.includes('Three')",
                   sliderInput("sliderOne", "Choose value no 1", 
                               min=0, max=50, value=25),
                   sliderInput("sliderThree", "Choose value no 3",
                               min=0, max=50, value=25))
)

# Define server logic 
server <- function(input, output) {
  output$myText <- renderText({input$choosemedia})
  
}
# Run the application 
shinyApp(ui = ui, server = server)

Any suggestion will be grately appreciated.

stefan
  • 90,330
  • 6
  • 25
  • 51
AussieAndy
  • 101
  • 2
  • 11

1 Answers1

2

Instead of adding multiple sliders and checking for just one case add only one slider per conditional panel and alter the condition to check for all cases for which the sliders should be displayed:

library(shiny)
myData <- c("One", "Two", "Three")

ui <- fluidPage(
  checkboxGroupInput("choosemedia", "Choose digital",
    choices  = myData,
    selected = myData
  ),
  textOutput("myText"),
  conditionalPanel(
    condition = "input.choosemedia.includes('One') || input.choosemedia.includes('Three')",
    sliderInput("sliderOne", "Choose value no 1",
      min = 0, max = 100, value = 50
    )
  ),
  conditionalPanel(
    condition = "input.choosemedia.includes('One') || input.choosemedia.includes('Two')",
    sliderInput("sliderTwo", "Choose value no 2",
                min = 0, max = 50, value = 25
    )
  ),
  conditionalPanel(
    condition = "input.choosemedia.includes('Three') ",
    sliderInput("sliderThree", "Choose value no 3",
      min = 0, max = 50, value = 25
    )
  )
)

# Define server logic
server <- function(input, output) {
  output$myText <- renderText({
    input$choosemedia
  })
}
# Run the application
shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:8851

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thank you!!! Although I need 6 check boxes and 16 sliders, so coding will be a bit messy, but it will work. Appreciate your help!!! – AussieAndy Oct 23 '22 at 00:44