Warning messages keep appearing in RStudio noteboo

2020-05-24 22:49发布

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)

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!

2条回答
甜甜的少女心
2楼-- · 2020-05-24 23:11

My experience is that this occurs in RStudio while my code still has the error, even if this is not run (for example, when I leave some wrong code to be revised later). When I delete, change or convert into comments the relevant lines, this behavior ends. I guess that this is caused by the RStudio interpreter. It would be interesting to know if people using base R have the same problem.

查看更多
手持菜刀,她持情操
3楼-- · 2020-05-24 23:14

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())

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.

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.

查看更多
登录 后发表回答