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
R: How to Find the Mode of a Vector
R - Ggplot2 Issues with Date as Character for X-Axis
Is There an R Function to Reshape This Data from Long to Wide
How to Replace Na (Missing Values) in a Data Frame with Neighbouring Values
Set the Size of Ggsave Exactly
How to Programmatically Extract/Unzip a .7Z (7-Zip) File with R
How to Manipulate the Strip Text of Facet_Grid Plots
How to Nicely Annotate a Ggplot2 (Manual)
When Should I Use Setdt() Instead of Data.Table() to Create a Data.Table
Standard Error Bars Using Stat_Summary
Density2D Plot Using Another Variable for the Fill (Similar to Geom_Tile)
Data.Table Row-Wise Sum, Mean, Min, Max Like Dplyr
Changing Million/Billion Abbreviations into Actual Numbers? Ie. 5.12M -> 5,120,000