Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I suppress the message of a function in an R script? [duplicate]

Tags:

r

rscript

A similar question was asked previously at:

How do I suppress this output?

However, that question is only applicable to Rmarkdown code blocks. In the following case I've created a script with only the following:

library(UsingR)

I've saved the file this time as an Rscript named test.R. However, I am using the Compile Notebook icon to compile to an html document. The resulting output is as shown:

enter image description here

In the previous question on the link given above, the answer was:

```{r message=FALSE}
library(UsingR)
```

which will only work in an Rmarkdown code block.

How do I suppress messages of functions more generally, for example the output of the library() function?

like image 945
David Avatar asked Dec 26 '22 05:12

David


1 Answers

You can change the call to library so that it doesn't print any output:

library(UsingR, quietly = TRUE, warn.conflicts = FALSE)

More generally, you can suppress messages from an expression by wrapping it in a suppressMessages:

suppressMessages(expr)
like image 67
Jonathan Avatar answered Feb 17 '23 20:02

Jonathan