实时更新ui值 - 闪亮

时间:2016-05-31 17:42:15

标签: r shiny

ui.r开始,我想将第二个sliderInput的最大值设为第一个sliderInput的当前值。我该怎么做呢?

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)

1 个答案:

答案 0 :(得分:2)

根据第一个滑块输入更新第二个滑块输入的示例代码。关键是

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

完整代码

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)