R 如何获得“警告”;如果相同的输出被使用两次,闪亮的应用程序将无法工作;
这是一个老问题: 一个简单的例子:R 如何获得“警告”;如果相同的输出被使用两次,闪亮的应用程序将无法工作;,r,shiny,R,Shiny,这是一个老问题: 一个简单的例子: library(shiny) ## app.R ## server <- function(input, output) { output$distPlot <- renderPlot({ hist(rnorm(input$obs), col = 'darkgray', border = 'white') }) } ui <- fluidPage( sidebarLayout( sidebarPanel(
library(shiny)
## app.R ##
server <- function(input, output) {
output$distPlot <- renderPlot({
hist(rnorm(input$obs), col = 'darkgray', border = 'white')
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100)
),
mainPanel(plotOutput("distPlot")
# If the next line is commented out, histogram appears correctly
,plotOutput("distPlot")
)
)
)
shinyApp(ui = ui, server = server)
Warning message: Output 'distPlot' is used twice in UI - this is not supported and might lead to unexpected results
虽然我知道这个问题,但我已经在复杂的UI中被欺骗过几次,我想知道是否有办法在控制台中输出关于这个问题的自动警告?例如:
library(shiny)
## app.R ##
server <- function(input, output) {
output$distPlot <- renderPlot({
hist(rnorm(input$obs), col = 'darkgray', border = 'white')
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100)
),
mainPanel(plotOutput("distPlot")
# If the next line is commented out, histogram appears correctly
,plotOutput("distPlot")
)
)
)
shinyApp(ui = ui, server = server)
Warning message: Output 'distPlot' is used twice in UI - this is not supported and might lead to unexpected results
感谢您在这个问题上分享您的解决方案 我认为将其打印到R控制台可能是一项相当艰巨的任务。但是,如果打开应用程序并转到JS控制台,您应该会看到一条错误消息:
按要求提供反馈(此处为答案,以便于格式化): 您的代码涵盖了基本情况,但也有一些您可能忽略的边缘情况(此列表并不是详尽无遗的):
outname假设大多数闪亮的UI输出遵循以下模式:
outputTypeOutput("outputname")
我编写了checkShinyOutput
函数,可以在UI脚本中的UI定义之前调用该函数:
checkShinyOutput <- function(){
tryCatch({
parsed <- getParseData(parse(file = rstudioapi::getSourceEditorContext()$path))
shinyOutput <- parsed[parsed$token=='SYMBOL_FUNCTION_CALL'& grepl("^[a-z]+[A-z]+Output$",parsed$text),]
shinyOutput <- merge(shinyOutput,parsed,by='line1')
shinyOutput <- shinyOutput[shinyOutput$token.y == "STR_CONST",]
warn <- table(shinyOutput$text.y)
warn <- warn[warn>=2]
warnname <- names(warn)
if (length(warn>1)) {
warning(mapply(function(warn,nb){paste("Output",warn,"is used",nb,"times")},warnname,warn))
}
},
error = function(){},
warning = function(cond) {
message("Shiny UI : check following warnings to avoid unexpected UI behaviour")
message(cond)
}
)
}
谢谢你的回答,我没有想到这一点,但它确实有意义。我将等待一段时间来接受它,因为控制台中的警告仍然更为明显。我想在JavaScript端创建一个错误处理程序,并通过shinny.setInputValue
通知R,但是,这种方法不起作用,因为显然中的错误阻止了shinny
javascript对象的正确实例化,因此我们无法通过此途径。对答案的反馈感兴趣我在我的答案中发布了反馈。感谢您的反馈,我知道它只涵盖基本情况,但到目前为止,这是我的全部使用;-)。但是同意你的看法,当出现问题时,只需看看js控制台。