Robert Hadow
Robert Hadow

Reputation: 459

Retrieving warnings generated in a knitr session

I have a long R Markdown script that generates more than fifty knitr errors. It produces a .pptx file that requires repairs (although I cannot see what needed repair). Occasionally PowerPoint renames the file based on the first title slide. The knitting session produces a warning message that invites me so inspect them by entering warnings(). Insofar as the knitr session is in an environment invisible to me, typing warnings() is not helpful.

Output created: demo.pptx There were 50 or more warnings (use warnings() to see the first 50)

I cannot give a succinct, reproducible example that produces knitr warnings, but I can tell you that they come from caret and gbm which spit out lots of formatted output (black) and unformatted messages (red) and even unwanted plots, despite the default setting of verbose = FALSE.

Here is the head of my .rmd file:

---
title: "Demo of knitr warnings when making Powerpoint"
author: "author name"
output:
  powerpoint_presentation:
    slide_level: 2
---
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width=10, echo=FALSE, warning=FALSE, message=FALSE, cache=FALSE)
```

```{r setup}   
suppressPackageStartupMessages(library(knitr))
suppressPackageStartupMessages(library(rmarkdown))
```

How can I discover the environment in which knitr executed, so I can access the warnings? When I tried to reproduce the warnings, I noted that the warnings generated by my code apparently aren't being appended to the last.warnings list.

Upvotes: 1

Views: 1194

Answers (2)

Robert Hadow
Robert Hadow

Reputation: 459

Yes! @user2554330 was right. rmarkdown::render("yourfile.Rmd") works fine, in your own global environment.

If you are using RStudio, there are a few things to keep in mind:

  • The source is whatever is saved to disk, so don't forget to save.
  • You can check the environment with knit.global(). Whether you type the command or use the knit button -- which apparently spawns a new R session -- the result will be the same. You can get to the first, but not to the second.

    environment: R_GlobalEnv

  • You won't see the objects in your local environment until execution is complete.
  • And PowerPoint doesn't start automatically.

Yes, all of the ugly warnings (in my case from gbm) are there.

Upvotes: 0

user2554330
user2554330

Reputation: 44788

Run knitr using rmarkdown::render("yourfile.Rmd") and it will run in the current session. Then warnings() will work.

For example, I didn't see any warnings from your code, but when I added

for ( i in 1:100)
  warning(i)

to the code chunk, I got a message like yours. So I used the idea above, and saw

> rmarkdown::render("~/temp/Untitled.Rmd")


processing file: Untitled.Rmd
  |................                                                 |  25%
  ordinary text without R code

  |................................                                 |  50%
label: global_options (with options) 
List of 1
 $ include: logi FALSE

  |.................................................                |  75%
  ordinary text without R code

  |.................................................................| 100%
label: setup

output file: Untitled.knit.md

'/Applications/RStudio 2.app/Contents/MacOS/pandoc/pandoc' +RTS -K512m -RTS Untitled.utf8.md --to pptx --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash+smart --output Untitled.pptx --slide-level 2 

Output created: Untitled.pptx
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In eval(expr, envir, enclos) : 1
2: In eval(expr, envir, enclos) : 2
3: In eval(expr, envir, enclos) : 3
4: In eval(expr, envir, enclos) : 4
5: In eval(expr, envir, enclos) : 5
6: In eval(expr, envir, enclos) : 6
7: In eval(expr, envir, enclos) : 7
8: In eval(expr, envir, enclos) : 8
9: In eval(expr, envir, enclos) : 9
10: In eval(expr, envir, enclos) : 10
11: In eval(expr, envir, enclos) : 11
12: In eval(expr, envir, enclos) : 12
13: In eval(expr, envir, enclos) : 13
14: In eval(expr, envir, enclos) : 14
15: In eval(expr, envir, enclos) : 15
16: In eval(expr, envir, enclos) : 16
17: In eval(expr, envir, enclos) : 17
18: In eval(expr, envir, enclos) : 18
19: In eval(expr, envir, enclos) : 19
20: In eval(expr, envir, enclos) : 20
21: In eval(expr, envir, enclos) : 21
22: In eval(expr, envir, enclos) : 22
23: In eval(expr, envir, enclos) : 23
24: In eval(expr, envir, enclos) : 24
25: In eval(expr, envir, enclos) : 25
26: In eval(expr, envir, enclos) : 26
27: In eval(expr, envir, enclos) : 27
28: In eval(expr, envir, enclos) : 28
29: In eval(expr, envir, enclos) : 29
30: In eval(expr, envir, enclos) : 30
31: In eval(expr, envir, enclos) : 31
32: In eval(expr, envir, enclos) : 32
33: In eval(expr, envir, enclos) : 33
34: In eval(expr, envir, enclos) : 34
35: In eval(expr, envir, enclos) : 35
36: In eval(expr, envir, enclos) : 36
37: In eval(expr, envir, enclos) : 37
38: In eval(expr, envir, enclos) : 38
39: In eval(expr, envir, enclos) : 39
40: In eval(expr, envir, enclos) : 40
41: In eval(expr, envir, enclos) : 41
42: In eval(expr, envir, enclos) : 42
43: In eval(expr, envir, enclos) : 43
44: In eval(expr, envir, enclos) : 44
45: In eval(expr, envir, enclos) : 45
46: In eval(expr, envir, enclos) : 46
47: In eval(expr, envir, enclos) : 47
48: In eval(expr, envir, enclos) : 48
49: In eval(expr, envir, enclos) : 49
50: In eval(expr, envir, enclos) : 50

Upvotes: 1

Related Questions