r - 摆脱 R 控制台中的“参数长度为零”错误消息
问题描述
我有下面的闪亮应用程序,其中显示了 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', "")
}
})
}
我用来检查andreq
的“真实性” 。顺便说一句,你不应该需要返回输入,因为它已经是.input$factors_weight_one
input$factors_weight_two
as.numeric
numericInput
numeric
推荐阅读
- node.js - 使用 MongoDB 的 NodeJS 嵌套组聚合
- qt - 在 QML 中使用 Brython
- python - 循环输入直到满足参数或用户输入
- c# - Json.NET TypeNameHandling 和 TypeNameAssemblyFormatHandling
- java - 创建包含收件人、主题、正文和附件的电子邮件
- c# - Unity 中的 C#:在不阻塞主线程的情况下调用 Promise 风格的异步方法
- ubuntu - Anki 上的 LATEX 无法正常显示;Ubuntu 18.10
- c# - 如果 2 个图像匹配,如何使 2 个图片框不可见
- java - 根据条件在jsp forloop中隐藏div标签
- c# - 多级属性的 nameof() 运算符