suppress package loading messages in R, disable package startup message in R

How to suppress package loading messages in R or warnings

If you want a cleaner look while running your code in interactive or batch mode, try to suppress package loading messages in R. There are also possible to disable some of the warnings. It is typically at the loading of chatty packages like dplyr or janitor. Of course, they have helpful information, and you should consider if there are any risks before disabling R messages.

 

Suppress package loading messages in R

Sometimes it is not necessary to see additional information that is after attaching a new package. For example, in the situation when you are running an R script in batch mode and want to display only the progress bar without additional clutter.

If you want a quick answer and don’t want to dig deeper, try function p_load from package pacman. You can easily load one or more packages at once and suppress loading messages or warnings.

pacman::p_load(tidyverse, progressr, janitor)

You can also use a list of R packages to load at once.

x <- c("tidyverse", "progressr", "janitor")

pacman::p_load(x, character.only = TRUE)

This is the way how to suppress startup messages from dplyr or any other R package. If you want to know alternatives or how to do that with base R, there are a couple of options below.

 

Load R packages quietly

If you have only one or a few packages like the tidyverse then here is one approach. Functions library or require contains quietly or warn.conflicts argument that ensures that most of the time there are no additional messages printed.

library(janitor, quietly = TRUE)

#Attaching package: ‘janitor’
#
#The following objects are masked from ‘package:stats’:
#  
#  chisq.test, fisher.test
#
#Warning message:
#  package ‘janitor’ was built under R version 3.6.3

In my situation, it was not working at all. The next thing to try is the function suppressPackageStartupMessages. Here is how the results look and you can compare them with the previous ones. R package loading messages are disabled, but there is also a warning.

suppressPackageStartupMessages(library(janitor))

#Warning message:
#package ‘janitor’ was built under R version 3.6.3

To disable the warning that comes with R package loading try this combination with function suppressWarnings.

suppressWarnings(suppressPackageStartupMessages(library(janitor)))

If you can use an additional package, then function p_load from package pacman can disable all previously seen loading messages.

pacman::p_load(janitor)

 

Load multiple packages at once and quietly in R

There are multiple ways how to load multiple packages without additional messages. A versatile way to do that is by using function p_load from package pacman. It suppresses loading messages and also warnings.

You can do that by defining necessary packages inside the p_load function.

pacman::p_load(progressr, janitor, dplyr)

If you like, you can use a separate list of necessary packages.

x <- c("progressr", "janitor", "dplyr")

pacman::p_load(x, character.only = TRUE)

Here is how to load multiple packages and suppress loading messages in base R.

suppressWarnings(suppressPackageStartupMessages({
  library(progressr)
  library(janitor)
  library(dplyr)
}))

 


Posted

in

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *