Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comment out some chunks/part of Rmd file

Is it possible to comment out part of a Rmd file which includes more than one chunk (eg: 4-5)? Regular HTML comments did not work.

---
title: "Untitled"
author: "author"
date: "5 August 2017"
output: pdf_document
---

```{r}
print(123)
```

```{r}
2**2
```
<!-- 
# Comment section starts

This text is not visible in the output.

```{r}
a <- 3*4
a
```
This text not be visible in the output.

# Comment section ends
-->

```{r}
print(1)
```

In the past, I remember reading somewhere in the SO posts that it is aimed in the next version of knitr.

Update: I am not looking for the solution to use eval=FALSE in every chunk as I need to comment out the text in between the chunks as well. Also, I am looking for an elegant way to do this.

The above code outputs pdf output as below:

enter image description here

Surprisingly, it works. But the same HTML comments (<!-- -->) does not work in another original Rmarkdown script. The part of skipping part of Rmd file is only achieved after including the below snippet surrounding the code I want to skip executing.

<!-- 
# Comment section starts

```{r, include=FALSE}
knitr::opts_chunk$set(eval= FALSE)
```

This is added to the end of the comment:
  ```{r, include=FALSE, eval=TRUE}
knitr::opts_chunk$set(eval= TRUE)
```
-->

Could some one explain to me what is the issue in this case?

like image 833
Prradep Avatar asked Sep 11 '17 04:09

Prradep


1 Answers

I wanted to post an updated answer to this as things have changed in newer versions. The html comments <!-- --> now achieve this. So everything between the html comment tags will not be run or included in the knitted document.

---
  title: "Untitled"
author: "author"
date: "5 August 2017"
output: pdf_document
---

  ```{r}
print(123)
```

```{r}
2**2
```
<!-- 
  # Comment section starts

  This text is not visible in the output.

```{r}
a <- 3*4
a
```
This text not be visible in the output.

# Comment section ends
-->

  ```{r}
print(1)
```
like image 182
jamesguy0121 Avatar answered Oct 11 '22 14:10

jamesguy0121