我在一个闪亮的应用程序中有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)
发布于 2018-04-11 06:57:52
您可以像这样添加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)
https://stackoverflow.com/questions/49766607
复制相似问题