28

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!

Javier Fajardo
  • 737
  • 1
  • 10
  • 22
  • 2
    This happens to me all the time. If I edit and save an rmd (I think that's the key), then the next command I enter in the console brings back every warning in the session 50x over. – Frank Apr 25 '17 at 14:58
  • 2
    Oh! So it doesn't happen only to me. Maybe it is just a feature to remind you that there are warning somewhere in your code. Thanks for your comment Frank! – Javier Fajardo Apr 25 '17 at 15:12
  • I think it happens when you cache a chunk the warnings come back later. I had that happen to me. – sconfluentus Apr 25 '17 at 15:28
  • That might be right, that was my case – Javier Fajardo Apr 25 '17 at 19:41
  • Related I think: https://stackoverflow.com/questions/42864262/persistent-warnings-from-earlier-commands (no real solution although some more speculation) – geotheory Jun 07 '17 at 16:39
  • 5
    Useful: `assign("last.warning", NULL, envir = baseenv())` – geotheory Jun 07 '17 at 16:59
  • Can you clarify whether you are running the chunks interactively or compiling the document at once? – IBrum Feb 23 '18 at 16:15

2 Answers2

3

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.

Katia
  • 3,784
  • 1
  • 14
  • 27
1

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.

JASC
  • 176
  • 3
  • 14