R:如何清除所有警告

发布于 2024-11-02 12:58:28 字数 235 浏览 0 评论 0原文

我想使用命令行清除 warnings() 列表。

我尝试过但没有成功,

> rm(last.warning, envir = baseenv())  
Error in rm(last.warning, envir = baseenv()) :   
cannot remove variables from the base environment

有什么想法吗?

I would like to clear the warnings() list using a command line.

I have tried with no success

> rm(last.warning, envir = baseenv())  
Error in rm(last.warning, envir = baseenv()) :   
cannot remove variables from the base environment

any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里南柯 2024-11-09 12:58:28

尝试分配(“last.warning”,NULL,envir = baseenv())

Try assign("last.warning", NULL, envir = baseenv())

古镇旧梦 2024-11-09 12:58:28

只是为了强调上面@Richie Cotton提到的内容(帮助页面现在不再提到2.4.0,但是):这实际上不是推荐,即使它已被接受的答案在这里。

相反,您应该使用更强大的错误处理程序工具,特别是对于上面的 @BWMorlan 的情况,您可以使用 - 多次“广告”
tryCatch.WE() 实用程序函数,它捕获所有警告和错误,并在没有错误时

r <- tryCatch.WE({ ... })

使用 R 中的 demo(error.keeping) 获取该函数并在中查看它行动,

file.show(system.file("demo/error.catching.R"))

获取评论来源。

Just to emphasize what @Richie Cotton mentioned above (the help page now no longer mentions 2.4.0, but): This is assign("last.warning", envir = baseenv()) is really not recommended even though it's been the accepted answer here.

Rather, you should use the much more powerful error handler tools, notably for @BWMorlan's case above, you could use the - several times "advertized"
tryCatch.WE() utility function which catches all warnings and errors and provides results when not error

r <- tryCatch.WE({ ... })

using demo(error.catching) in R to get the function and see it in action,
or

file.show(system.file("demo/error.catching.R"))

to get the commented source.

亽野灬性zι浪 2024-11-09 12:58:28

看看 suppressWarnings()< /code>停止显示警告。

警告帮助页面中的通知它说:

“...没有记录在哪里
last.warning 已存储,也不是
可见,这取决于
改变。在 R 2.4.0 之前它被存储
在工作区中,但不再......”

Take a look at suppressWarnings() to stop the warnings from showing up.

Notice in the help page for warnings that it says:

"....It is undocumented where
last.warning is stored nor that it is
visible, and this is subject to
change. Prior to R 2.4.0 it was stored
in the workspace, but no longer...."

白昼 2024-11-09 12:58:28

我同意,我想使用 try() 并仅收集该 try() 生成的警告。

我现在的解决方案是

assign("last.warning", NULL, envir = baseenv())
    myFit  <- try(...)
    warned <- warnings()
assign("last.warning", NULL, envir = baseenv())

I agree, I want to use a try() and gather up just the warnings generated by that try().

My solution for now is

assign("last.warning", NULL, envir = baseenv())
    myFit  <- try(...)
    warned <- warnings()
assign("last.warning", NULL, envir = baseenv())
︶葆Ⅱㄣ 2024-11-09 12:58:28

只需使用“警告(立即。= FALSE)”
例如

x<-matrix(1:100,ncol=10)
plot(x,border="blue")
    Warning messages:
    1: In plot.window(...) : "border" is not a graphical parameter
    2: In plot.xy(xy, type, ...) : "border" is not a graphical parameter
    3: In axis(side = side, at = at, labels = labels, ...) :
      "border" is not a graphical parameter
    4: In axis(side = side, at = at, labels = labels, ...) :
      "border" is not a graphical parameter
    5: In box(...) : "border" is not a graphical parameter
    6: In title(...) : "border" is not a graphical parameter
warnings()
    Warning messages:
    1: In plot.window(...) : "border" is not a graphical parameter
    2: In plot.xy(xy, type, ...) : "border" is not a graphical parameter
    3: In axis(side = side, at = at, labels = labels, ...) :
      "border" is not a graphical parameter
    4: In axis(side = side, at = at, labels = labels, ...) :
      "border" is not a graphical parameter
    5: In box(...) : "border" is not a graphical parameter
    6: In title(...) : "border" is not a graphical parameter
> warning(immediate. = FALSE)
    Warning message:
> warnings()
    Warning message:

Simply use 'warning(immediate. = FALSE)'
E.g.

x<-matrix(1:100,ncol=10)
plot(x,border="blue")
    Warning messages:
    1: In plot.window(...) : "border" is not a graphical parameter
    2: In plot.xy(xy, type, ...) : "border" is not a graphical parameter
    3: In axis(side = side, at = at, labels = labels, ...) :
      "border" is not a graphical parameter
    4: In axis(side = side, at = at, labels = labels, ...) :
      "border" is not a graphical parameter
    5: In box(...) : "border" is not a graphical parameter
    6: In title(...) : "border" is not a graphical parameter
warnings()
    Warning messages:
    1: In plot.window(...) : "border" is not a graphical parameter
    2: In plot.xy(xy, type, ...) : "border" is not a graphical parameter
    3: In axis(side = side, at = at, labels = labels, ...) :
      "border" is not a graphical parameter
    4: In axis(side = side, at = at, labels = labels, ...) :
      "border" is not a graphical parameter
    5: In box(...) : "border" is not a graphical parameter
    6: In title(...) : "border" is not a graphical parameter
> warning(immediate. = FALSE)
    Warning message:
> warnings()
    Warning message:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文