R - Markdown avoiding package loading messages
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)
Related videos on Youtube
Roark
Updated on August 08, 2020Comments
-
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 over 6 yearsset
options(warn=-1)
and back tooptions(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 about 9 yearsThe fact that
results
can't beFALSE
is unfortunate. I would rather the chunk optionresults
be split intoresults
,hold
, andmarkup
which would all be boolean values. Alas, this is not how it works. -
Prasad Chalasani about 9 yearswhat if we want to turn off messages globally for ALL chunks, how do you do that?
-
Yihui Xie about 9 years@PrasadChalasani
knitr::opts_chunk$set(message = FALSE)
yihui.name/knitr/options -
Alex P. Miller over 8 yearsThe
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 about 7 yearsHow to set this for the entire document?. Do i have to put it into each chunk manually?.
-
cbare about 7 years@Misaki See Yihui's comment above regarding knitr::opts_chunk$set(message = FALSE)
-
Yihui Xie almost 6 years@Head
results
can beFALSE
(which is an alias ofresults="hide"
) since knitr 1.16: github.com/yihui/knitr/issues/1360 -
sjd about 3 yearsWhen i put this format on top of my script i got
unexpected symbol in "{r results"