4

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?

DryLabRebel
  • 8,923
  • 3
  • 18
  • 24
David
  • 981
  • 1
  • 15
  • 27
  • No. The first occurred in an RMarkdown file compiled using knitHTML. This one occurred in an R script, clicking the Compile Notebook icon in RStudio. Similar, but different. – David Aug 24 '14 at 20:10
  • 1
    Irrelevant. It is about suppressing output from `library() ` and that question has been answered several times. – Dirk Eddelbuettel Aug 24 '14 at 20:11
  • Are we relitigating _seven year old_ answers? Didn't anything else worth arguing happen in the meantime? – Dirk Eddelbuettel May 18 '21 at 23:12
  • (sorry for deleting my comment) I realise now what you meant. Though I do think this question is different enough to remove the duplicate flag. I only bring it up because the question, and answer provided are relevant to my current situation, whereas the linked QnA is not. I'm just trying to be helpful. -- I also realise this has been answered in other places, but I think the answer here is correct, and the same answer for other questions is less relevant. – DryLabRebel May 18 '21 at 23:29

1 Answers1

11

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)
Jonathan
  • 8,497
  • 41
  • 35
  • 1
    library(UsingR,quietly=TRUE,warn.conflicts=FALSE) did not work. Got the same output. suppressMessages(library(UsingR)) worked. Thanks. – David Aug 24 '14 at 19:02
  • @Jonathan this is super useful, thanks. In particular I had a function that needs to be called thousands of time and each time would print a redundant message() that could jam up the console. – Ahdee Jan 12 '19 at 16:15