根据另一个输入约束一个闪亮的应用程序输入

时间:2017-02-08 19:55:10

标签: r shiny

我有一个基本的闪亮应用评估 A + 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")
)

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

shinyApp(ui = ui, server = server)

A numericInput值, B sliderInput值。

我想约束我的应用,以便 B 的最大输入值始终为2 * A 。因此,我必须将max =中的硬编码sliderInput更改为可动态的内容。我怎么能做到这一点?

由于

2 个答案:

答案 0 :(得分:6)

您可以致电updateSliderInput,在observe内更改B的最大值,只要A更改,该值就会被触发:

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)

答案 1 :(得分: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)