Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable messages upon loading a package

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?

like image 314
learner Avatar asked Dec 30 '11 16:12

learner


People also ask

What is the use of R packages?

R packages are a collection of R functions, complied code and sample data. They are stored under a directory called "library" in the R environment. By default, R installs a set of packages during installation. More packages are added later, when they are needed for some specific purpose.


2 Answers

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>  
like image 178
Dirk Eddelbuettel Avatar answered Sep 28 '22 04:09

Dirk Eddelbuettel


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

like image 27
Mehrad Mahmoudian Avatar answered Sep 28 '22 05:09

Mehrad Mahmoudian