238

I have been using Knitr via R-Studio, and think it is pretty neat. I have a minor issue though. When I source a file in an R-Chunk, the knitr output includes external comments as follows:

+ FALSE Loading required package: ggplot2
+ FALSE Loading required package: gridExtra
+ FALSE Loading required package: grid
+ FALSE Loading required package: VGAM
+ FALSE Loading required package: splines
+ FALSE Loading required package: stats4
+ FALSE Attaching package: 'VGAM'
+ FALSE The following object(s) are masked from 'package:stats4':

I have tried to set R-chunk options in various ways but still didn't seem to avoid the problem:

```{r echo=FALSE, cache=FALSE, results=FALSE, warning=FALSE, comment=FALSE, warning=FALSE} 
source("C:/Rscripts/source.R");

```

Is there any way to comment out these messages?

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
Roark
  • 2,575
  • 2
  • 14
  • 8
  • 2
    set `options(warn=-1)` and back to `options(warn=0)` at the end of the Rmd. Takes care of all startup package messages. Note that you'd be turning off warnings, but only while the Rmd is being rendered. – Dan Kalleward Nov 08 '17 at 17:03

4 Answers4

312

You can use include=FALSE to exclude everything in a chunk.

```{r include=FALSE}
source("C:/Rscripts/source.R")
```

If you only want to suppress messages, use message=FALSE instead:

```{r message=FALSE}
source("C:/Rscripts/source.R")
```
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • The fact that `results` can't be `FALSE` is unfortunate. I would rather the chunk option `results` be split into `results`, `hold`, and `markup` which would all be boolean values. Alas, this is not how it works. – Head Mar 17 '15 at 16:55
  • 2
    what if we want to turn off messages globally for ALL chunks, how do you do that? – Prasad Chalasani May 07 '15 at 00:10
  • 18
    @PrasadChalasani `knitr::opts_chunk$set(message = FALSE)` http://yihui.name/knitr/options/ – Yihui Xie May 07 '15 at 04:53
  • 1
    @Head `results` can be `FALSE` (which is an alias of `results="hide"`) since **knitr** 1.16: https://github.com/yihui/knitr/issues/1360 – Yihui Xie Aug 07 '18 at 04:46
120
```{r results='hide', message=FALSE, warning=FALSE}
library(RJSONIO)
library(AnotherPackage)
```

see Chunk Options in the Knitr docs

cbare
  • 12,060
  • 8
  • 56
  • 63
  • 13
    The `warning=FALSE` is the only thing that worked for me. Definitely the best way to include the chunk in your output without the ugly warnings! – Alex P. Miller Sep 18 '15 at 14:05
  • How to set this for the entire document?. Do i have to put it into each chunk manually?. – Fadwa Mar 03 '17 at 12:32
  • 3
    @Misaki See Yihui's comment above regarding knitr::opts_chunk$set(message = FALSE) – cbare Mar 03 '17 at 23:16
  • When i put this format on top of my script i got `unexpected symbol in "{r results"` – sjd Mar 12 '21 at 15:39
  • Yeah I tend to do the same, a chunk just to load libraries, in R chunk like `{r global, echo = FALSE, message = FALSE, warning = FALSE}` – thomasswilliams Nov 24 '22 at 09:14
7

My best solution on R Markdown was to create a code chunk only to load libraries and exclude everything in the chunk.

{r results='asis', echo=FALSE, include=FALSE,}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE)
#formating tables
library(xtable)

#data wrangling
library(dplyr)

#text processing
library(stringi)
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Paul Tyler
  • 103
  • 1
  • 6
7

This is an old question, but here's another way to do it.

You can modify the R code itself instead of the chunk options, by wrapping the source call in suppressPackageStartupMessages(), suppressMessages(), and/or suppressWarnings(). E.g:

```{r echo=FALSE}
suppressWarnings(suppressMessages(suppressPackageStartupMessages({
source("C:/Rscripts/source.R")
})
```

You can also put those functions around your library() calls inside the "source.R" script.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96