andandandand
andandandand

Reputation: 22270

Real time updating of ui values - Shiny

From ui.r, I want to make the max value of the second sliderInput the current value of the first sliderInput. How do I do this?

sliderInput("blockSize", "block size", min = 1, max = 12, value = 8, step = 1)

#max should be the current value of blockSize instead of 12
sliderInput("offset", "offset", min = 1, max = 12 , value = 1, step = 1)

Upvotes: 1

Views: 939

Answers (1)

Xiongbing Jin
Xiongbing Jin

Reputation: 12087

Sample code that updates the second slider input depending on the first one. The key is

observeEvent(input$bins, {
    updateSliderInput(session, "bins2", max=input$bins)
})

Complete code

library(shiny)

ui <- shinyUI(fluidPage(

   titlePanel("Old Faithful Geyser Data"),

   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),
         sliderInput("bins2",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)
      ),

      mainPanel(
         plotOutput("distPlot")
      )
   )
))

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

    observeEvent(input$bins, {
        updateSliderInput(session, "bins2", max=input$bins)
    })

   output$distPlot <- renderPlot({
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
})

shinyApp(ui = ui, server = server)

Upvotes: 2

Related Questions