34

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?

mchangun
  • 9,814
  • 18
  • 71
  • 101
  • 1
    This answered my problem with a error that wouldn't be suppressed http://stackoverflow.com/questions/24978427/suppressing-error-messages-in-knitr – Timo Kvamme Aug 13 '15 at 21:04

3 Answers3

45

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

user190477
  • 551
  • 1
  • 4
  • 3
40

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

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
mnel
  • 113,303
  • 27
  • 265
  • 254
  • +1 I didn't know that tidbit of information about `dev.off()` and knitr. Thanks! – A5C1D2H2I1M1N2O1R2T1 Mar 14 '13 at 10:13
  • 21
    please be sure to include my kitten poster when you see `dev.off()`: http://yihui.name/en/2012/06/enjoyable-reproducible-research/ – Yihui Xie Mar 14 '13 at 21:54
  • 3
    @yihui - I was searching for that kitten poster (without success). I've included it now. – mnel Mar 14 '13 at 22:00
  • 1
    but what if you want to output an image to a file so you can use it elsewhere? What's the alternative to dev.off()? – Nova Jul 12 '18 at 17:12
  • 1
    @Nova I was looking for that question as well. If you are fine with exporting all figures to a folder (which I think `rmarkdown::render` does by default) you can add a general settings option like `knitr::opts_chunk$set(dev = c('png', 'pdf'),fig.path='figures/')` where in dev you specify which output formats you need. – FM Kerckhof Oct 08 '18 at 10:25
20

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.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • 4
    Still looking for a way to suppress warnings for ggplot calls. Wrapping ggplot call into invisible suppress the image as well, which is kind of counter productive. :) – Roman Luštrik Apr 21 '13 at 09:39
  • Hi, I have the same problem with using "ksvm". Unfortunately, "invisible" does not work. – CodingButStillAlive Oct 21 '16 at 10:25
  • Worth adding this `invisible` solution https://stackoverflow.com/a/61571259/4083743, the benefit is that you can wrap certain lines of code in a chunk using `invisible` and `capture.output` - meaning you dont have to wrap everything – user63230 Mar 06 '23 at 19:14