0
votes

Friends could help me make my conditionalPanel functional. I made a conditionalPanel however I don't know how to adjust it on the server. When I press the option "No" I would like it to show the sliderInput ("Slider2"). The executable code is below. Thank you!

library(shiny)

ui <- shiny::navbarPage(
  title = div(tags$img(src="", align="right", height='50px')),
              sidebarLayout(
               sidebarPanel(
                 sliderInput("Slider1",
                             "Number of bins:",
                             min = 1,
                             max = 50,
                             value = 30)
               ),

               sidebarLayout(
                 sidebarPanel(
                   radioButtons("filter1","", choices = list("Yes" = 1,"No " = 2),selected = 1),
                   conditionalPanel(
                      "input.filter1 == 'No'",

                    sliderInput("Slider2",
                               "Number of bins:",
                               min = 1,
                               max = 20,
                               value = 30)),
                 ),
                 mainPanel(
                   plotOutput("distPlot1")
                 ))))


server <- function(input, output) {

  output$distPlot1 <- renderPlot({
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$Slider1 + 1)


    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

2

2 Answers

1
votes

When you do

radioButtons("filter1", "", choices = list("Yes" = 1,"No " = 2)

the values of the radio buttons are "1" and "2", while "Yes" and "No" are the labels of the radio buttons. So you condition should be "input.filter1 == '2'".

0
votes

Your condition must be "input.filter1 == 2" and not "input.filter1 == 'No'".
"No" is the name of the element, while 2 is the value (that is evaluated).

It will work with this modification.