Suppress package startup messages

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.

library(dplyr, warn.conflicts = FALSE)

Surround a chatty library() call with suppressPackageStartupMessages().

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.

#+ 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.

library(tidyverse)

slice(iris, 1)

tidyverse_quiet also silences startup messages from the tidymodels meta-package.

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.

library(dplyr)
unloadNamespace("dplyr")

warn.conflicts = FALSE

To suppress warnings about conflicts, set the warn.conflicts argument of library() to FALSE.

library(dplyr, warn.conflicts = FALSE)

slice(iris, 1)
unloadNamespace("dplyr")

suppressPackageStartupMessages()

Surround library() with suppressPackageStartupMessages().

suppressPackageStartupMessages(library(dplyr))

slice(iris, 1)
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:

```r  
library(dplyr)

wzxhzdk:11


## 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.



wzxhzdk:12



wzxhzdk:13


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. The `tidyverse_quiet` argument and `reprex.tidyverse_quiet` option also affect startup messages from the [tidymodels](https://www.tidymodels.org) meta-package.
      
      

Try the reprex package in your browser

Any scripts or data that you put into this service are public.

reprex documentation built on Aug. 17, 2022, 9:07 a.m.