删除 R 控制台中的'argument is of length zero'错误消息



我有一个闪亮的应用程序,其中显示了2个数字输入。该应用程序运行良好,因为当总和不为40时,会显示错误消息。什么是烦人的,我想摆脱的是错误消息

Warning: Error in if: argument is of length zero

当我第一次运行应用程序时,它会出现在r控制台中。我知道这来自line 38,并且与一开始的NULL值有关。有趣的是,当我不将renderUI()用于2个数字输入时,不会显示此错误消息。但在我的实际情况下,我需要他们是这样的。

library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
#This hides the temporary warning messages while the plots are being created
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
uiOutput("factors_weight_one_two"),
htmlOutput('weight_message')
),
mainPanel(
)
)
)
server <- function(input, output) {
output$factors_weight_one_two <- renderUI({
fluidRow(
column(6, numericInput(
"factors_weight_one",
"Factor 1", 20, 
min = 1, max = 100,
width = "90%")),
column(6, numericInput(
"factors_weight_two",
"Factor 2", 20, 
min = 1, max = 100,
width = "90%"))
)
})
output$weight_message <- renderText({
if(!is.null(as.numeric(input$factors_weight_one) + as.numeric(input$factors_weight_two) ) & as.numeric(input$factors_weight_one) + as.numeric(input$factors_weight_two)  != 40){
sprintf('<font color="%s">%s</font>', 'red', "Your weights don't sum to 40")
}  else {
sprintf('<font color="%s">%s</font>', 'red', "")
}
})
}
shinyApp(ui, server)

server部分改成这个怎么样

server <- function(input, output) {
output$factors_weight_one_two <- renderUI({
fluidRow(
column(6, numericInput(
"factors_weight_one",
"Factor 1", 20,
min = 1, max = 100,
width = "90%")),
column(6, numericInput(
"factors_weight_two",
"Factor 2", 20,
min = 1, max = 100,
width = "90%"))
)
})
output$weight_message <- renderText({
req(input$factors_weight_one, input$factors_weight_two)
if (input$factors_weight_one + input$factors_weight_two  != 40) {
sprintf('<font color="%s">%s</font>', 'red', "Your weights don't sum to 40")
}  else {
sprintf('<font color="%s">%s</font>', 'red', "")
}
})
}

我使用req来检查input$factors_weight_oneinput$factors_weight_two的"真实性"。顺便说一句,numericInput的返回输入不需要as.numeric,因为它已经是numeric了。

相关内容

最新更新