【问题标题】:How do I suppress warnings with plotly?如何使用 plotly 抑制警告?
【发布时间】:2021-02-08 00:31:45
【问题描述】:

我想在不关闭全局警告的情况下停止警告

此代码将进入一个函数,因此它可以在不同的会话中运行

这是我的代码

      fit <-
        plot_ly(credit_old, 
            y = ~score, 
            color = ~fte, 
            type = "box")
      suppressWarnings(fit)   

我还是得到了

Warning messages:
1: In RColorBrewer::brewer.pal(N, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels

2: In RColorBrewer::brewer.pal(N, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    suppressWarnings(fit) 返回一个值为fit。然后会自动打印此结果,这就是显示警告的原因。和这个类似

    result <- suppressWarnings(fit) # No warning here
    print(result) # Warning here
    #> Warning messages:
    #> 1: In min(x, na.rm = na.rm) :
    #>   no non-missing arguments to min; returning Inf
    #> 2: In max(x, na.rm = na.rm) :
    #>   no non-missing arguments to max; returning -Inf
    

    您可以在suppressWarnings 中调用print。这不会导致自动打印,因为print 会不可见地返回它的值。

    suppressWarnings(print(fit))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-11
      • 2019-02-25
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      相关资源
      最近更新 更多