【问题标题】:Update sliderInput in Shiny reactively以反应方式更新 Shiny 中的 sliderInput
【发布时间】:2017-04-13 07:38:09
【问题描述】:

我正在尝试动态更改 sliderInput 的值。现在的困难是我想从一个值的sliderInput 更改为一个范围的sliderInput,这似乎不起作用。

下面代码中的第一个操作按钮可以工作,而第二个操作按钮则不能正常工作。

是切换到uiOutput 元素的唯一可能性吗?

代码

library(shiny)
app <- shinyApp(
   ui = bootstrapPage(
      sliderInput("sld1", min = 0, max = 10, label = "y1", value = 5),
      actionButton("acb1", "Change Value"),
      actionButton("acb2", "Change Value to Range")
   ),
   server = function(input, output, session) {
      observeEvent(input$acb1, {
         updateSliderInput(session, "sld1", value = 2)
      })
      observeEvent(input$acb2, {
         updateSliderInput(session, "sld1", value = c(2,7))
      })
   })
runApp(app)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以使用renderUI动态添加滑块

    #rm(list = ls())
    library(shiny)
    app <- shinyApp(
      ui = bootstrapPage(
        uiOutput("myList"),
        actionButton("acb1", "Change Value"),
        actionButton("acb2", "Change Value to Range")
      ),
      server = function(input, output, session) {
    
        slidertype <- reactiveValues()
        slidertype$type <- "default"
    
        observeEvent(input$acb1,{slidertype$type <- "normal"})
        observeEvent(input$acb2, {slidertype$type <- "range"})
    
        output$myList <- renderUI({
    
          if(slidertype$type == "normal"){
            sliderInput("sld1", min = 0, max = 10, label = "y1", value = 2)
          }
          else if(slidertype$type == "range"){
            sliderInput("sld1", min = 0, max = 10, label = "y1", value = c(2,7))
          }
          else{
            sliderInput("sld1", min = 0, max = 10, label = "y1", value = 5)
          }
        })    
    })
    runApp(app)
    

    【讨论】:

    • 嗯,这就是我的想法,我必须依赖uiOutput - 感谢我们提供的示例,尽管reactiveValues 的用例很好
    猜你喜欢
    • 2020-06-26
    • 2020-04-23
    • 1970-01-01
    • 2018-08-02
    • 2019-10-19
    • 2016-05-17
    • 2017-03-25
    • 2023-02-25
    • 2019-04-26
    相关资源
    最近更新 更多