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
Comma Separator for Numbers in R
How to Assign the Result of the Previous Expression to a Variable
Convert Currency with Commas into Numeric
Overlay Data Onto Background Image
Using 'Geom_Line()' with X Axis Being Factors
Sum All Values in Every Column of a Data.Frame in R
Find the Intersection of Overlapping Ranges in Two Tables Using Data.Table Function Foverlaps
Remove All Rows Where Length of String Is More Than N
Print "Pretty" Tables for H2O Models in R
Write Many Files in a for Loop
Convert Yyyymmdd String to Date Class in R
Assign Intermediate Output to Temp Variable as Part of Dplyr Pipeline
"Correct" Way to Specifiy Optional Arguments in R Functions