R - Markdown avoiding package loading messages

253,077

Solution 1

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")
```

Solution 2

```{r results='hide', message=FALSE, warning=FALSE}
library(RJSONIO)
library(AnotherPackage)
```

see Chunk Options in the Knitr docs

Solution 3

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.

Solution 4

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)
Share:
253,077

Related videos on Youtube

Roark
Author by

Roark

Updated on August 08, 2020

Comments

  • Roark
    Roark almost 4 years

    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?

    • Dan Kalleward
      Dan Kalleward over 6 years
      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.
  • Head
    Head about 9 years
    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.
  • Prasad Chalasani
    Prasad Chalasani about 9 years
    what if we want to turn off messages globally for ALL chunks, how do you do that?
  • Yihui Xie
    Yihui Xie about 9 years
    @PrasadChalasani knitr::opts_chunk$set(message = FALSE) yihui.name/knitr/options
  • Alex P. Miller
    Alex P. Miller over 8 years
    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!
  • Fadwa
    Fadwa about 7 years
    How to set this for the entire document?. Do i have to put it into each chunk manually?.
  • cbare
    cbare about 7 years
    @Misaki See Yihui's comment above regarding knitr::opts_chunk$set(message = FALSE)
  • Yihui Xie
    Yihui Xie almost 6 years
    @Head results can be FALSE (which is an alias of results="hide") since knitr 1.16: github.com/yihui/knitr/issues/1360
  • sjd
    sjd about 3 years
    When i put this format on top of my script i got unexpected symbol in "{r results"