Disable messages upon loading a package

RMessageLibrariesSuppress WarningsVerbosity

R Problem Overview


I have a package in R (ROCR) that I need to load in my R environment. Upon loading the package, a set of messages are printed. This is ordinarily fine, but since the output of my R script is being used for further analysis I want to completely disable all of this output. How do I do that? Furthermore, I'd prefer to do it without having to modify ROCR at all, so that future users of this script don't have to do that either.

So far:

  • sink() doesn't work here - redirecting both stdout and std err to /dev/null does nothing for me.
  • Unsurprisingly, options(warnings=-1) does nothing either, since these are not warnings, per se, being printed.

Any thoughts?

R Solutions


Solution 1 - R

Just use suppressMessages() around your library() call:

edd@max:~$ R

R version 2.14.1 (2011-12-22)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)
[...]

R> suppressMessages(library(ROCR))
R>                                               # silently loaded
R> search() 
 [1] ".GlobalEnv"         "package:ROCR"         # it's really there      
 [3] "package:gplots"     "package:KernSmooth"
 [5] "package:grid"       "package:caTools"   
 [7] "package:bitops"     "package:gdata"     
 [9] "package:gtools"     "package:stats"     
[11] "package:graphics"   "package:grDevices" 
[13] "package:utils"      "package:datasets"  
[15] "package:methods"    "Autoloads"         
[17] "package:base"      
R> 

Solution 2 - R

Dirk's answer suppresses all messages and is not specific to messages that is generated while loading packages.

The more accurate solution to the asked question is:

suppressPackageStartupMessages(library(THE_PACKAGE_NAME))

A bit more detailed explanation can be found here

Solution 3 - R

Use suppressPackageStartupMessages, see the answer by MehradMahmoudian. For completeness, adding here examples of usage:

For one library, use suppressPackageStartupMessages(...), for example:

suppressPackageStartupMessages(library(ggplot2))

For multiple libraries, use suppressPackageStartupMessages({...}), for example:

suppressPackageStartupMessages({
    library(ggplot2)
    library(ggdendro)
})

SEE ALSO:
Suppress package startup messages

Solution 4 - R

library(ROCR, quietly = TRUE) might be a more elegant option.

Solution 5 - R

By adding quietly = T as shown below will solve the issue:

suppressWarnings(suppressMessages(library("dplyr", quietly = T)))

In case of multiple package you can use :

## specify the package names
PKGs <- c("affy","gcrma","readxl","ggplot2","lattice" )

and them use lapply as below:

lapply(PKGs, library, character.only = TRUE ,quietly = T)

Solution 6 - R

If you load packages with a for loop, then you have to silent the complete loop like I show below instead of suppressing the message when loading the library individually.

requiredPackages = c('plyr','dplyr','data.table')
suppressMessages(
 for (p in requiredPackages) {
  if (!require(p, character.only = TRUE)){
   install.packages(p)
  }
  library(p, character.only = TRUE)
 }
)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlearnerView Question on Stackoverflow
Solution 1 - RDirk EddelbuettelView Answer on Stackoverflow
Solution 2 - RMehrad MahmoudianView Answer on Stackoverflow
Solution 3 - RTimur ShtatlandView Answer on Stackoverflow
Solution 4 - RAndrei MartinsView Answer on Stackoverflow
Solution 5 - RYousefView Answer on Stackoverflow
Solution 6 - RVicky RuizView Answer on Stackoverflow