Update sliderInput in Shiny reactively

送分小仙女□ 提交于 2020-01-01 17:27:10

问题


I am trying to change the values from a sliderInput dynamically. The difficulty now is that I want to change from a sliderInput with one value, to a sliderInput with a range, which seems not to work.

The first actionbutton in the code below works, while the second does not what it is intended to do.

Is the only possibility to switch to an uiOutput element?

Code

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)

回答1:


You can maybe add the slider dynamically using 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)


来源:https://stackoverflow.com/questions/40863663/update-sliderinput-in-shiny-reactively

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!