Suppressing Messages in Knitr/Rmarkdown

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.

How to suppress all messages in knitr/ Rmarkdown?

I think you want the chunk option results='hide' for that particular code chunk only.

```{r results='hide'}
# do your dplyr computation here
```

Suppressing Error Messages in knitr

Errors have their own dedicated hook function, stored in the environment accessed by knit_hooks$get(). Here, for your enlightenment, is the complete list of those functions:

names(knit_hooks$get())
# [1] "source" "output" "warning" "message" "error" "plot"
# [7] "inline" "chunk" "document"

To suppress warnings, just overwrite the default error hook function with one that takes the required arguments, but doesn't return anything at all.

\documentclass{article}
\begin{document}

<<setup, include=FALSE, cache=FALSE>>=
muffleError <- function(x,options) {}
knit_hooks$set(error=muffleError)
@

<<Test>>=
1:10
X
@
\end{document}

Which, upon compilation, yields the following

Sample Image

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


Related Topics



Leave a reply



Submit