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

eme*_*hex 3 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)
Run Code Online (Sandbox Code Playgroud)

AnumericInput值,BsliderInput值.

我想我的限制应用程序,以便为最大输入值始终是2*一个.因此,我必须硬编码的改变max =sliderInput的东西,可以是动态的.我怎么能做到这一点?

谢谢

Hub*_*rtL 7

您可以调用updateSliderInput以更改B中的最大值,observe只要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")
)

# 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)
Run Code Online (Sandbox Code Playgroud)


Big*_*ist 5

你正在寻找 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)
Run Code Online (Sandbox Code Playgroud)