How to eliminate library warnings and messages using knitr

Hello,

I have tried many things to eliminate the warnings and comments produced by the libraries with no success. Is there any thing that might do the trick? This is the last command i tried.

suppressMessages(library(dplyr, warn.conflict = FALSE, quietly = TRUE))

Try the knitr code chunk options message = FALSE and warning = FALSE; see slides here:

https://ysc-rmarkdown.netlify.com/slides/01-basics.html#75

```{r message=FALSE, warning=FALSE}
library(tidyverse)
```
1 Like

I used the command you sent but still get warnings and messages:

```{r message=FALSE, warning=FALSE}
library(dplyr)

source("C:/Users/giuse/OneDrive/Documents/DataScience/R SCRIPTS/STORM DATA/severe_storms.R")

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

filter, lag

The following objects are masked from 'package:base':

intersect, setdiff, setequal, union

Attaching package: 'gridExtra'

The following object is masked from 'package:dplyr':

combine

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

last_plot

The following object is masked from 'package:stats':

filter

The following object is masked from 'package:graphics':

layout

Storing 'username' as the environment variable 'plotly_username'

Storing 'key' as the environment variable 'plotly_api_key'

Warning: 'plotly' is deprecated.

Use 'ggplotly' instead.

See help("Deprecated")

Warning: 'plotly' is deprecated.

Use 'plot_ly' instead.

See help("Deprecated")

I think using include=FALSE in the chunk arguments will remove those messages from the output (see here for details).

1 Like

This will mean that the code to load the package will also not be included in the output. If you don't want that printed, that is a good option.

But, I am unable to reproduce the poster's original issue with the information given. When I use warning and message set to FALSE for a code chunk that calls library(dplyr), I don't see any warnings or messages printed.

But what the poster is seeing are lots of other warnings too (like for plotly). So I suspect these are produced by the call to the sourced .R script (which I cannot reproduce). You'll need to apply those code chunk options globally or at the least to the chunk that loads that .R script, which must load other packages and contain code that prints warnings and messages. For example, if I include this chunk in an R Markdown document:

```{r message=FALSE, warning=FALSE}
source("myscript.R")
```

Where myscript.R loads the dplyr package only, no warnings or messages are printed when I knit to HTML. You can also set these chunk options globally in a setup chunk so that they apply to all following code chunks, as shown here:

https://ysc-rmarkdown.netlify.com/slides/01-basics.html#82

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.