警告消息始终以与警告无关的块形式出现在RStudio笔记本中 [英] Warning messages keep appearing in RStudio notebooks in chunks unrelated to the warnings
问题描述
我开始使用RStudio笔记本电脑,但我仍在尝试了解某些功能的工作原理. 我不明白为什么在执行与该消息完全无关的代码时,为什么会保留某些产生的警告消息并出现这些警告消息.例如,我有一个包含多个块的文档,其中最后一个产生警告
I am starting to use RStudio notebooks, and I am still trying to understand how some of the things work. I do not understand why some produced warning messages are kept and appear when executing code that is completely unrelated to the message. For instance, I have a document with several chunks, where the last of them produces the warning
> warnings()
Warning messages:
1: Unknown or uninitialised column: 'perc.goal.met.period'.
2: Unknown or uninitialised column: 'perc.goal.met.period'.
3: Unknown or uninitialised column: 'perc.goal.met.period'.
4: Unknown or uninitialised column: 'perc.goal.met.period'.
5: Unknown or uninitialised column: 'perc.goal.met.period'.
6: Unknown or uninitialised column: 'perc.goal.met.period'.
7: Unknown or uninitialised column: 'perc.goal.met.period'.
8: Unknown or uninitialised column: 'perc.goal.met.period'.
9: Unknown or uninitialised column: 'perc.goal.met.period'.
10: Unknown or uninitialised column: 'perc.goal.met.period'.
11: Unknown or uninitialised column: 'perc.goal.met.period'.
12: Unknown or uninitialised column: 'perc.goal.met.period'.
13: Unknown or uninitialised column: 'perc.goal.met.period'.
14: Unknown or uninitialised column: 'perc.goal.met.period'.
15: Unknown or uninitialised column: 'perc.goal.met.period'.
16: Unknown or uninitialised column: 'perc.goal.met.period'.
17: Unknown or uninitialised column: 'perc.goal.met.period'.
18: Unknown or uninitialised column: 'perc.goal.met.period'.
19: Unknown or uninitialised column: 'perc.goal.met.period'.
20: Unknown or uninitialised column: 'perc.goal.met.period'.
21: Unknown or uninitialised column: 'perc.goal.met.period'.
22: Unknown or uninitialised column: 'perc.goal.met.period'.
23: Unknown or uninitialised column: 'perc.goal.met.period'.
24: Unknown or uninitialised column: 'perc.goal.met.period'.
25: Unknown or uninitialised column: 'perc.goal.met.period'.
26: Unknown or uninitialised column: 'perc.goal.met.period'.
27: Unknown or uninitialised column: 'perc.goal.met.period'.
28: Unknown or uninitialised column: 'perc.goal.met.period'.
29: Unknown or uninitialised column: 'perc.goal.met.period'.
30: Unknown or uninitialised column: 'perc.goal.met.period'.
There were 30 warnings (use warnings() to see them)
我可以接受该警告.但是后来,我以为我会向第一个块(我在其中加载它们)加载一个额外的库.运行该块后,我得到:
I am ok with that warning. But later, I thought I would load one additional library to the first of the chunks (where I load them). After running that chunk, I get:
```{r echo=F, message=F, warnings=F, include=F}
# Load libraries
library(rgdal)
library(raster)
library(openxlsx)
library(tidyverse)
library(dplyr)
library(magrittr)
library(ggplot2)
library(rasterVis)
```
There were 30 warnings (use warnings() to see them)
如果我看到警告,则它们是我之前打印的警告.
我为什么在这里见到他们?我还在其他与变量perc.goal.met.period
不相关的块中也看到了这一点.如果我看到警告,它们将在一段时间内停止出现,但在我仍然无法预料的时刻,它们最终将在某个时候再次出现.
If I see the warnings, they are those I printed before.
Why am I seeing them here? I am seeing this also in other chunks also unrelated to the variable perc.goal.met.period
. If I see the warnings, they will stop appearing for a while, but at a moment that I am still not able to anticipate, they will eventually reappear at some point.
对此行为是否有合乎逻辑的解释?非常感谢您的帮助!
Is there a logical explanation for this behaviour? Thanks a lot for your help!
推荐答案
您将看到警告消息,直到将其清除.运行warnings()函数不会执行此操作. 要清除警告,您可以执行以下命令:
You will see warning messages until you clear them out. Running warnings() function does not do that. To clear warnings you can execute the following command:
assign("last.warning", NULL, envir = baseenv())
尽管,最好的方法是修复代码,以免产生警告.解决它的一种方法是在R中使用tryCatch()
.
The best approach though is to fix your code so the warnings are not produced. One way to deal with it is to use tryCatch()
in R.
您还可以使用supressWarnings()
功能禁用所有警告,但是不建议您这样做,因为这会阻止您看到其中的任何警告.
You can also disable all warnings by using supressWarnings()
function, but this is not recommended since this will prevent you to see any of them.
这篇关于警告消息始终以与警告无关的块形式出现在RStudio笔记本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!