Making shiny app UI elements dynamic

Hello everyone!! I've been having trouble with making my app dynamic. I want a sliderInput to appear in the UI only if the option Histograma is selected from the selectInput options. I'd really appreciate any help. I attach a simple example below. In this example the sliderInput appears by default, and I want it to display in the app only if the the Histograma option is selected.

library(shiny)

ui <- fluidPage(
  
  br(),
  
  selectInput(inputId = "tipograf", label = "Seleccione el tipo de gráfico",
              choices = c("LĂ­nea", "Box-plot", "Histograma")),
  
  uiOutput("selectBins")
  
  
)




server <- function(input, output, session) {

observeEvent("input$tipoGrafico == Histograma", {
  
  output$selectBins <- renderUI({
  
  conditionalPanel(condition = "input.tipograf == Histograma",
                   sliderInput(inputId = "numBins", label = "Seleccione el nĂşmero de barras",
                               min = 10, max = 100, value = 20, step = 1))
  })
})

}

shinyApp(ui = ui, server = server)

Hi. This should work. The conditionalPanel is on the ui side and histograma is quoted.

library(shiny)

ui <- fluidPage(
  
  br(),
  
  selectInput(inputId = "tipograf", label = "Seleccione el tipo de gráfico",
              choices = c("LĂ­nea", "Box-plot", "Histograma")),
  conditionalPanel(condition = "input.tipograf == 'Histograma'",
                   uiOutput("selectBins")
  )
  
)

server <- function(input, output, session) {
  
  output$selectBins <- renderUI({
    sliderInput(inputId = "numBins", label = "Seleccione el nĂşmero de barras",
                min = 10, max = 100, value = 20, step = 1)

  })
  
}

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

It works!! Thank you very much William.

1 Like

This topic was automatically closed 54 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.