R - Markdown Avoiding Package Loading Messages

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")
```

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>

How to suppress messages in R chunk output?

The message option takes a logical argument (i.e., TRUE/FALSE): See knitr documentation.

This sould work:

```{r, warning=FALSE, results='hide',message=FALSE}
x <- c("ggmap", "rgdal", "rgeos", "maptools", "dplyr", "tidyr", "tmap")
lapply(x, library, character.only = TRUE) # load the required packages
```

Suppressing messages in Knitr / Rmarkdown

Try using invisible to suppress those types of output.

```{r}
invisible(getSymbols("^RUT"))
chart.TimeSeries(RUT)
invisible(dev.off())
```

From the help page for ?invisible:

This function can be useful when it is desired to have functions return values which can be assigned, but which do not print when they are not assigned.

Neither of these are "messages" or "warnings", but actual output values. You'll see that the messages for getSymbols are, indeed, suppressed by knitr in the output.

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.



Related Topics



Leave a reply



Submit