Disable Messages Upon Loading a Package

Disable messages upon loading a package

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>

Suppress package loading messages

Not sure if anyone is still looking for an answer to this one but

suppressWarnings(suppressMessages(library("dplyr")))

works perfectly on Jupyter Notebooks. I typically define a function like this:

import_library = function(lib_name){
suppressWarnings(suppressMessages(require(lib_name, character.only = TRUE)))
}
import_library('dplyr')

Note that, inside the user-defined function, library(...) will not work, so use require(...). The character.only = TRUE is also necessary to circumvent R from trying to load lib_name as name of library, rather than actual library (in our case dplyr).

A similar answer can be found here.

Suppress loading messages when calling function directly (not loading package in full)

Thought I'd tried this before but apparently not. Both of these options work:

p <- suppressMessages(raster::rasterToPolygons(r, dissolve = TRUE))
p <- suppressPackageStartupMessages(raster::rasterToPolygons(r, dissolve = TRUE))

I call the function explicitly using :: (advised by Hadley here), but you can also avoid the rgeos loading message by importing it in the NAMESPACE of your package. If using roxygen2, that means adding @import rgeos as an roxygen2 comment at the top of your function. I imagine @importFrom rgeos fun would work too, but I don't know which rgeos functions are being used by raster::rasterToPolygons.

How to suppress warning messages when loading a library?

These are not messages but warnings. You can do:

suppressWarnings(library(RODBC))

or

suppressWarnings(suppressMessages(library(RODBC)))

to suppress both types.

R - Markdown avoiding package loading messages

You can use include=FALSE to exclude everything in a chunk.

```{r include=FALSE}
source("C:/Rscripts/source.R")
```

If you only want to suppress messages, use message=FALSE instead:

```{r message=FALSE}
source("C:/Rscripts/source.R")
```

how to stop a package message when we load it?

An option is to capture the printed version as a string with capture.output and then wrap with invisible. The suppressPackageStartupMessages will prevent the printing of usual messages of attaching other packages/masking of functions (if any) and whatever left over was printed gets captured and removed with invisible

invisible(capture.output(suppressPackageStartupMessages(library(biotools)))) 

load multiple packages AND supress messages

One option would be

pacman::p_load(packages)


Related Topics



Leave a reply



Submit