【问题标题】:Suppress warning message in R console of shiny抑制闪亮的R控制台中的警告消息
【发布时间】:2014-08-30 10:02:44
【问题描述】:

我们开发了一个闪亮的应用程序。它显示了一些警告消息,我们从不打扰它,因为应用程序运行良好。但是,我们无法分发应用程序并在控制台中显示警告消息。现在我的问题是,当闪亮的应用程序运行时,如何在 R 控制台中抑制警告消息。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    在您的 ui 脚本中插入它。

    tags$style(type="text/css",
             ".shiny-output-error { visibility: hidden; }",
             ".shiny-output-error:before { visibility: hidden; }"
    )
    

    【讨论】:

      【解决方案2】:

      实际上,您通过 R 有两个抑制功能,它们对您的工作方式不同:

      suppressMessages()忽略所有“简单”诊断消息的上下文中评估其表达式。”参见?suppressMessages

      • suppressWarnings()忽略所有警告的上下文中评估其表达式。见?suppressWarnings

      例子:

      f <- function(a) { a ; warning("This is a warning") ; message("This is a message not a warning")}
      
      > f(1)
      This is a message not a warning
      Warning message:
      In f(1) : This is a warning
      
      > suppressWarnings(f(1))
      This is a message not a warning
      
      > suppressMessages(f(1))
      Warning message:
      In f(1) : This is a warning
      

      【讨论】:

        【解决方案3】:

        suppressWarnings 包裹在您的代码周围应该可以工作。见?suppressWarnings。您需要执行以下操作:

        atest <- function(n) {warning("a warning"); return(n+1)}
        atest(1)
        #[1] 2
        #Warning message:
        #In atest(2) : a warning
        
        suppressWarnings(atest(1))
        #[1] 2
        

        但我想更好的解决方案是实际处理警告而不是忽略它。

        【讨论】:

        • 您还可以通过 CSS 隐藏与闪亮相关的消息。 .shiny-output-error { visibility: hidden; }.shiny-output-error:before { visibility: hidden; }。警告仍然在后端生成,只是没有向用户显示。
        猜你喜欢
        • 2012-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-25
        • 2013-03-16
        • 2016-07-05
        相关资源
        最近更新 更多