2016-11-29 81 views
2

我試圖動態更改sliderInput的值。現在的困難是,我想從一個值爲sliderInput改爲sliderInput,這個範圍似乎不起作用。更新sliderInput中Shiny reactively

下面的代碼中的第一個actionbutton起作用,而第二個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 = 2:7) 
     }) 
    }) 
runApp(app) 

回答

4

你也許可以添加使用動態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) 
+0

好滑,那是我想,我必須依靠'uiOutput' - 感謝來回摸索出例如雖然有一個很好的用例'reactiveValues' – thothal