2017-02-08 44 views
1

我有一個基本的有光澤的應用程序,用於評估 + 光澤應用輸入:約束基於另一輸入

library(shiny) 

ui <- fluidPage(
    numericInput(inputId = "A", label = "A", value = 5, step = 1), 
    sliderInput(inputId = "B", label = "B", min = 0, max = 10, value = 5), 
    textOutput(outputId = "value") 
) 

server <- function(input, output) { 
    output$value <- renderText(paste0("A + B = ", input$A + input$B)) 
} 

shinyApp(ui = ui, server = server) 

numericInput值和sliderInput值。

我想約束我的應用程序,以便B的最大輸入值始終爲2 * A。因此,我必須將sliderInput中的硬編碼max =更改爲可動態的內容。我怎樣才能做到這一點?

感謝

+1

所有這些信息都在Shinys教程頁面上,除此之外,還有一些類似的問題已經在SO上提出,只是讓sur e你做你的研究,因爲它不會給社區帶來任何新鮮事物 –

回答

3

您正在尋找renderUI()

library(shiny) 

ui <- fluidPage(
    numericInput(inputId = "A", label = "A", value = 5, step = 1), 
    uiOutput("slider"), 
    textOutput(outputId = "value") 
) 

server <- function(input, output) { 
    output$value <- renderText(paste0("A + B = ", input$A + input$B)) 
    output$slider <- renderUI({ 
    sliderInput(inputId = "B", label = "B", min = 0, max = 2*input$A, value = 5) 
    }) 
} 

shinyApp(ui = ui, server = server) 
4

您可以致電updateSliderInputobserve內改變B中的最大值將被觸發每當一個變化:

library(shiny) 

ui <- fluidPage(
    numericInput(inputId = "A", label = "A", value = 5, step = 1), 
    sliderInput(inputId = "B", label = "B", min = 0, max = 10, value = 5), 
    textOutput(outputId = "value") 
) 

# Notice the session argument to be passed to updateSliderInput 
server <- function(input, output, session) { 
    output$value <- renderText(paste0("A + B = ", input$A + input$B)) 
    observe(updateSliderInput(session, "B", max = input$A*2)) 
} 

shinyApp(ui = ui, server = server)