Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
139 lines (94 sloc) 3.93 KB
---
title: "Suppress package startup messages"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Suppress package startup messages}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
Sometimes your reprex uses packages that emit messages and warnings at startup (dplyr is a very common culprit). In general, these are worth reading! They can alert you to the root cause of your problem, such as a function in one package masking a function in another. But in many cases, this is just distracting, startup noise.
How can you silence this chatter, specifically? We don't want to suppress messages and warnings, in general, because they are an important part of the reprex.
## TL;DR
Here's a quick look at various techniques. They are described in more detail below.
Call `library()` with `warn.conflicts = FALSE`.
```{r, eval = FALSE}
library(dplyr, warn.conflicts = FALSE)
```
Surround a chatty `library()` call with `suppressPackageStartupMessages()`.
```{r, eval = FALSE}
suppressPackageStartupMessages(library(dplyr))
```
Break your reprex into "chunks", in the `.Rmd` sense, and use a special `#+` comment to silence messages and/or warnings for the chunk that holds a chatty `library()` call. Note that the second `#+` comment is very important, so you don't silence messages and warnings for the entire reprex.
```{r, eval = FALSE}
#+ message = FALSE, warning = FALSE
library(dplyr)
#+
slice(iris, 1)
```
If you're using one or more tidyverse packages, consider using the tidyverse metapackage, literally. `reprex::reprex()` has an argument `tidyverse_quiet`, which defaults to `TRUE` and silences the startup messages.
```{r, eval = FALSE}
library(tidyverse)
slice(iris, 1)
```
## dplyr is chatty at startup
dplyr is a common culprit for noisy startup, so we use it as an example. Note this messaging as a baseline.
```{r}
library(dplyr)
```
```{r include = FALSE}
unloadNamespace("dplyr")
```
## `warn.conflicts = FALSE`
To suppress warnings about conflicts, set the `warn.conflicts` argument of `library()` to `FALSE`.
```{r}
library(dplyr, warn.conflicts = FALSE)
slice(iris, 1)
```
```{r include = FALSE}
unloadNamespace("dplyr")
```
## `suppressPackageStartupMessages()`
Surround `library()` with `suppressPackageStartupMessages()`.
```{r}
suppressPackageStartupMessages(library(dplyr))
slice(iris, 1)
```
```{r include = FALSE}
unloadNamespace("dplyr")
```
## Set `message = FALSE` and `warning = FALSE` for a chunk
If we were working in R Markdown, we could suppress messages and warnings in the chunk containing `library()` calls, then put our "real code" in a different chunk:
<pre><code>```{r, message = FALSE, warning = FALSE}
library(dplyr)
```
Some text.
`r ''````{r}
slice(iris, 1)
```</code></pre>
We can do the same in plain R code, suitable for `reprex()`ing, by using special comments that start with `#+`. Note that the second `#+` is significant, because it begins a new chunk capable of emitting messages and warnings.
```{r eval = FALSE}
#+ message = FALSE, warning = FALSE
library(dplyr)
message("You CANNOT hear me!")
#+
message("You can hear me!")
slice(iris, 1)
```
## reprex knows about `tidyverse_quiet`
The `reprex::reprex()` function has a `tidyverse_quiet` argument that defaults to `TRUE`. If your reprex uses one or more tidyverse packages, consider attaching the tidyverse metapackage, instead of individual packages, in order to enjoy a quiet startup.
<!-- faking this so I don't have to install tidyverse just for pkgdown -->
```{r eval = FALSE}
library(tidyverse) # instead of library(dplyr)
slice(iris, 1)
```
```{r echo = FALSE}
suppressPackageStartupMessages(library(dplyr))
slice(iris, 1)
```
Note that this default behaviour can be overridden by setting `tidyverse_quiet = FALSE` in a specific `reprex()` call or by setting the option `reprex.tidyverse_quiet = FALSE` in the `.Rprofile` startup file.
You can’t perform that action at this time.