mchangun
mchangun

Reputation: 10322

Suppressing messages in Knitr / Rmarkdown

Here is the code for my RMarkdown file:

```{r echo=FALSE, message=FALSE}
opts_chunk$set(comment = NA, echo=FALSE, message = FALSE, warnings = FALSE)
options("getSymbols.warning4.0"=FALSE)
Sys.setenv(TZ = "GMT")
library(quantmod)
library(xtable)
library(PerformanceAnalytics)
```

```{r}
getSymbols("^RUT")
chart.TimeSeries(RUT)
dev.off()
```

Despite settings message = FALSE, warnings = FALSE, I am still getting output messages in the HTML file when I run getSymbols() and dev.off(). Their respective outputs are:

[1] "RUT"

and

null device 
          1 

How do I suppress these messages?

Upvotes: 34

Views: 55344

Answers (3)

user190477
user190477

Reputation: 541

Ran into this problem as well, I would like to add that it should be warning = FALSE, not warnings = FALSE

Upvotes: 44

mnel
mnel

Reputation: 115382

You should never need to use dev.off() when using knitr. It takes care of all the devices to create plots.

From the package author Yihui

God kills a kitten whenever you dev.off()

null device 
          1 

Is the output of dev.off().

It may be that getSymbols returns something given that you haven't defined env

If you want to hide the results (output) (in general) you can use results = 'hide' as an option. No need to wrap anything in invisible()

Upvotes: 40

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

Try using invisible to suppress those types of output.

```{r}
invisible(getSymbols("^RUT"))
chart.TimeSeries(RUT)
invisible(dev.off())
```

From the help page for ?invisible:

This function can be useful when it is desired to have functions return values which can be assigned, but which do not print when they are not assigned.

Neither of these are "messages" or "warnings", but actual output values. You'll see that the messages for getSymbols are, indeed, suppressed by knitr in the output.

Upvotes: 20

Related Questions