107

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?

Timur Shtatland
  • 7,599
  • 2
  • 20
  • 30
learner
  • 1,686
  • 2
  • 17
  • 21
  • What messages are being printed? –  Dec 30 '11 at 16:42
  • 1
    While the entire output is unnecessary, the first several lines are: `Loading required package: gplots Loading required package: gtools Loading required package: gdata` - Note that it won't let me format the message correctly. – learner Dec 30 '11 at 16:52
  • Then load `gplots`, `gtools`, and `gdata` before loading `ROCR`. –  Dec 30 '11 at 16:58
  • 2
    @Jack, nope, see my answer below for the proper fix. – Dirk Eddelbuettel Dec 30 '11 at 17:10
  • @DirkEddelbuettel - That works too. TIMTOWTDI. –  Dec 30 '11 at 17:14
  • No Jack, not really. `suppressMessages()` also suppresses noisy textual message, at least from packages that conform, and protects you against other packages adding new dependencies which you'd need to reflect manually. – Dirk Eddelbuettel Dec 30 '11 at 17:46
  • 6
    Obligatory "its a package, not a library" comment here – Spacedman Dec 30 '11 at 18:49

5 Answers5

156

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> 
Dirk Eddelbuettel
  • 331,520
  • 51
  • 596
  • 675
  • 24
    Aren't you supposed to use `suppressPackageStartupMessages`? – hadley Dec 31 '11 at 00:24
  • 11
    No net gain apart from spending X more characters. Plus `suppressPackageStartupMessages` suppresses only _startup messages_ where as my preferred `suppressMessages()` suppresses any and all messages (but not `cat()` as I recall). But one is not supposed to use that in startup text. – Dirk Eddelbuettel Dec 31 '11 at 00:36
  • 1
    It's also great that Dirk's suggestion suppress messages from depending packages as well when they're being loaded. – Matt Bannert May 12 '16 at 15:24
  • 2
    Is there a way to capture those startup messages? I can capture warnings and errors, but don't know how to capture other such messages. – Adrian Oct 01 '16 at 19:57
  • 1
    @Adrian yes there is, wrap into tryCatch and provide message handler – jangorecki Apr 05 '19 at 12:58
25

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

Mehrad Mahmoudian
  • 2,681
  • 25
  • 35
19

Use suppressPackageStartupMessages, see the answer by @MehradMahmoudian. For completeness, adding 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)
})
Timur Shtatland
  • 7,599
  • 2
  • 20
  • 30
3

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)
Shawn
  • 3,313
  • 7
  • 42
  • 58
Yousef
  • 54
  • 5
2

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