Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何获得“警告”;如果相同的输出被使用两次,闪亮的应用程序将无法工作;_R_Shiny - Fatal编程技术网

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控制台,您应该会看到一条错误消息:


按要求提供反馈(此处为答案,以便于格式化):

您的代码涵盖了基本情况,但也有一些您可能忽略的边缘情况(此列表并不是详尽无遗的):

  • 如果输出元素的名称在下一行中,代码将不会检测到它
  • 如果元素的名称不是字符串,而是在变量中,那么它也不会起作用
  • (这只是我能很快想到的两个案例——老实说,它们并非完全不切实际)

    总的来说,我认为要涵盖所有情况需要相当多的工作,而快速查看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控制台。