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

下面的代码中的第一个actionbutton起作用,而第二个则不打算这样做。

是切换到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)

最佳答案

您也许可以使用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)

关于r - react 性更新Shiny中的sliderInput,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40863663/

10-12 17:14