1

I want to execute the following R chunk, but when I generate the PDF of the RMarkdown I don't want to include the Loading messages.

Knitr Setup chunk

{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) knitr::opts_knit$set(progress=FALSE)

Load Packages

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

Output I'm getting

enter image description here

Wanted Output

Only the chunk in the pdf without messages of loading packages.

Fran Martinez
  • 677
  • 4
  • 15
  • 36
  • 2
    This question is not a duplicate as the suggested answer in the referenced question I'm supposedly duplicating says to set include= FALSE. I don't want to hide the chunk... – Fran Martinez Mar 15 '18 at 02:10

3 Answers3

4

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
```
JBGruber
  • 11,727
  • 1
  • 23
  • 45
1

There is a base function suppressPackageStartupMessages that serves this purpose. Wrapping your expression will prevent the text from printing to the console.

x <- c("ggmap", "rgdal", "rgeos", "maptools", "dplyr", "tidyr", "tmap")
suppressPackageStartupMessages(lapply(x, library, character.only = TRUE))
Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40
  • This works fine but will show up in the knitted document. – JBGruber Mar 14 '18 at 22:33
  • 1
    @JonGrub, if you look at the duplicate answer that I linked to, you will see you could have a separate chunk to load your packages and set the chunk option `include=FALSE`, which is the standard approach. – Kevin Arseneau Mar 14 '18 at 22:36
1

I use the pacman library

pacman::p_load(ggmap, rgdal, rgeos, maptools, dplyr, tidyr, tmap)
cephalopod
  • 1,826
  • 22
  • 31