【问题标题】:How to validate multiple numeric inputs in Shiny and display an error message?如何验证 Shiny 中的多个数字输入并显示错误消息?
【发布时间】:2018-04-11 14:57:51
【问题描述】:

我在 Shiny 应用中有 3 个数字输入。这些是具有最小和最大阈值的百分比。显然总和不应大于 100。

当 3 个输入的总和大于 100 时,如何添加错误消息或通知?

代码如下:

library(shiny)

# Define the UI
ui <- bootstrapPage(
  numericInput('s1', 'Share 1 (%):', 30, min = 5, max = 55),
  numericInput('s2', 'Share 2 (%):', 30, min = 5, max = 55),
  numericInput('s3', 'Share 3 (%):', 40, min = 5, max = 55),

  textOutput('result')
)


# Define the server code
server <- function(input, output) {
  output$result <- renderText({
    (input$s1 + input$s2 + input$s3)
  })
}


# Return a Shiny app object
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以像这样添加validate

    library(shiny)
    
    # Define the UI
    ui <- bootstrapPage(
      numericInput('s1', 'Share 1 (%):', 30, min = 5, max = 55),
      numericInput('s2', 'Share 2 (%):', 30, min = 5, max = 55),
      numericInput('s3', 'Share 3 (%):', 40, min = 5, max = 55),
    
      textOutput('result')
    )
    
    
    # Define the server code
    server <- function(input, output) {
      output$result <- renderText({
        out <- input$s1 + input$s2 + input$s3
        validate(
          need(out <= 100, "The sum can't be over 100")
        )
        out
      })
    }
    
    # Return a Shiny app object
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多