Suppress messages from ALL calls to load package
2
0
Entering edit mode
@elizabeth-purdom-2486
Last seen 6 weeks ago
USA/ Berkeley/UC Berkeley

Hello, I am wondering if there is a way to suppress ALL start up messages in loading packages. In other words, I have a lot of library calls, like so:

require(splines)
require(ggplot2)
require(dplyr)
require(tidyverse)
require(reshape2)
require(knitr)
require(MASS)
require(moanin)
require(topGO)


I would like for none of these to print messages about conflicts or the "Welcome to Bioconductor" message etc. BUT I don't want to put suppressPackageStartupMessages in front of every call (and remember to do it for every new library I might add in the future).

For context, this is not interactive code. This is code run on the command line (via a makefile) using Rscript and these messages really clog up my output. Moreover, I have a large pipeline of code, so I have a slew of such existing .R/.Rmd files to compile, so in addition to not wanting to have the messy code, I don't want to manually have to add suppressPackageStartupMessages manually to each call in each file. I'd rather add a single line of code at the top of each existing file.

Finally, I know that in Rmarkdown, I can set the option message=FALSE, so it doesn't show up in my compiled output. However, I am not only running .Rmd files. Furthermore, even for those that are .Rmd, I am running them via Rscript so that while those messages don't show up in the html compiled file, it does get output to the terminal (not sure why) hence making very cluttered output when I'm trying to review the output of my calls that I've piped to a log file.

Thanks so much for any suggestions!

Elizabeth Purdom

suppressPackageStartupMessages Biobase • 272 views
2
Entering edit mode
@herve-pages-1542
Last seen 4 days ago
Seattle, WA, United States

Hi Elizabeth,

You don't need to put suppressPackageStartupMessages in front of every call. Putting it once in front of all the calls should be enough:

suppressPackageStartupMessages({
library(splines)
library(ggplot2)
library(dplyr)
etc...
})


Also it's safer to use library() rather than require() as the latter will only issue a warning if a package is missing while the former will raise an error. Better to get a hard failure early rather than after several hours of computation ;-)

Cheers,

H.

0
Entering edit mode
@james-w-macdonald-5106
Last seen 20 hours ago
United States

> startThat <- function(package) suppressPackageStartupMessages(require(package, character.only = TRUE))
> startThat("splines")
> startThat("ggplot2")

0
Entering edit mode

While technically possible I'd discourage this as for example renv expect standard syntax such as library() or require() to detect packages for its snapshotting: https://github.com/rstudio/renv/issues/720