Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
9 views
in R Programming by (5.3k points)
edited by

I have been using Knitr via R-Studio, and think it is pretty neat. I have a minor issue though. When I source a file in an R-Chunk, the knitr output includes external comments as follows:

+ FALSE Loading required package: ggplot2

+ FALSE Loading required package: gridExtra

+ FALSE Loading required package: grid

+ FALSE Loading required package: VGAM

+ FALSE Loading required package: splines

+ FALSE Loading required package: stats4

+ FALSE Attaching package: 'VGAM'

+ FALSE The following object(s) are masked from 'package:stats4':

I have tried to set R-chunk options in various ways but still didn't seem to avoid the problem:

```{r echo=FALSE, cache=FALSE, results=FALSE, warning=FALSE, comment=FALSE, warning=FALSE} 

source("C:/Rscripts/source.R");

```

Is there any way to comment out these messages?

2 Answers

0 votes
by
edited by

To avoid package loading messages, you can suppress the warnings temporarily as follows:

defaultW <- getOption("warn") 

options(warn = -1) 

YOUR CODE 

options(warn = defaultW)

You can also use the include = FALSE to exclude everything in a chunk.i.e.,

```{r include=FALSE}

install.packages("ggplot2")

```

To only suppress messages:

```{r message=FALSE}

install.packages("ggplot2")

```

0 votes
by (1.9k points)

When you attach functions or external packages, the messages you are seeing are probably generated as output from the source() call. You can make use of the capabilities, suppressPackageStartupMessages() and suppressMessages(), by modifying the source() call so that startup and routine messages will be silenced.

Try using this wrapper for your source() call:

suppressPackageStartupMessages(suppressMessages(source("C:/Rscripts/source.R")))


 

Also, configure your chunk options to not display messages and warnings:

suppressPackageStartupMessages(suppressMessages(source("C:/Rscripts/source.R")))


 

Related questions

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...