2017-10-16 68 views

回答

2

我wuld建議使用功能updateSliderInputupdateTextInput。這些功能讓你更新給定值的元素像這樣

updateSliderInput(session, "slider_id", value = c(0,1)) 
updateTextInput(session, "text_id", placeholder = "placeholder") 

另外,您還可以使用renderUI,但在大多數usecases,更新,功能應該是首選的性能的原因。

下面的工作解決方案創建了名爲controledSlider的閃亮模塊。該模塊將minmaxvalue作爲參數,並顯示滑塊,兩個文本框和一個操作按鈕。

library(shiny) 

controlledSliderUI <- function(id){ 
    ns = NS(id) 
    wellPanel(
    sliderInput(ns("slider"), NULL, 0, 1, c(0, 1)), 
    textInput(ns("min"), "min", 0, "50%"), 
    textInput(ns("max"), "max", 100, "50%"), 
    actionButton(ns("update"), "update slider") 
) 
} 

controlledSlider <- function(input, output, session, min, max, value){ 
    reactiveRange <- reactiveValues(min = value[1], max = value[2]) 
    updateSliderInput(session, "slider", min = min, max = max) 

    ## observe slider 
    observeEvent(input$slider,{ 
    reactiveRange$min <- input$slider[1] 
    reactiveRange$max <- input$slider[2] 
    }, ignoreInit = TRUE) 

    ## observe button 
    observeEvent(input$update,{reactiveRange$min <- as.numeric(input$min)}) 
    observeEvent(input$update,{reactiveRange$max <- as.numeric(input$max)}) 

    ## observe reactive 
    observeEvent({reactiveRange$min; reactiveRange$max},{ 
    updateSliderInput(
     session, "slider", value = c(reactiveRange$min, reactiveRange$max)) 
    updateTextInput(session, "min", value = reactiveRange$min) 
    updateTextInput(session, "max", value = reactiveRange$max) 
    }) 

    return(reactiveRange) 
} 

該模塊返回可讀取並從主服務器功能更新reactiveValue對象。

shinyApp(
    fluidPage(
    controlledSliderUI("mySlider"), 
    verbatimTextOutput("text") 
), 
    function(input, output, session){ 
    range <- callModule(controlledSlider, "mySlider", 0, 1200, c(100,1000)) 
    range$max <- 1001 ## update max 
    output$text <- renderPrint({ 
     print(range$min) 
     print(range$max) 
    }) 
    } 
) 

enter image description here