Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug (or at least unexpected behavior) for code chunks commented out w/ HTML comments #720

Closed
jgellar opened this issue Jun 8, 2016 · 8 comments
Labels
RStudio IDE concerns the rstudio ide
Milestone

Comments

@jgellar
Copy link

jgellar commented Jun 8, 2016

I'm not sure if this is a bug in rmarkdown, knitr, or RStudio, but i'm posting it here. I'm using rmarkdown version 0.9.6, knitr version 1.12.3, and Rstudio version 0.99.893. Also, I'm running MRO version 3.2.5, if that matters at all.

It appears that if you comment out a section of an R markdown document using HTML comments, and that section contains an R code chunk, then that code chunk is still executed when you knit the document. The execution is silent, so any print statements/figures do not appear in the output. However, if you modify variables in the chunk, they do get modified.

Note that if you "run all previous chunks in the source file" from a chunk after the commented-out section, so that the code is executed in the console, the commented-out chunk does not get executed. This is the behavior I would expect to happen when you knit the document. Here's a reproducible example:


---
title: "Bug when using HTML comments"
author: "Jonathan Gellar"
date: "June 8, 2016"
output: html_document

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

I am going to demonstrate a small bug that occurs when using HTML comments. First, I'll generate some data:
```{r gendata}
x <- 1:5
x
```

That was easy. Now I'm going to comment out a section using HTML comments.

<!--
This section will be commented out and won't appear in the output file. Now I insert a code chunk:
```{r moddata}
x <- 6:10
print(x)
```

-->

Now we are done with the commented section. Let's print x and see what's there:
```{r print}
x
```
@jjallaire
Copy link
Member

That's correct. knitr treats your document as a set of text with chunks in
it (it doesn't know about comments). If you want to comment out the R code
at this point you just need to comment it out using R comment syntax (or
just add eval=FALSE) to it.

On Wed, Jun 8, 2016 at 3:33 PM, jgellar notifications@github.com wrote:

I'm not sure if this is a bug in rmarkdown, knitr, or RStudio, but i'm
posting it here. I'm using rmarkdown version 0.9.6, knitr version 1.12.3,
and Rstudio version 0.99.893. Also, I'm running MRO version 3.2.5, if that
matters at all.

It appears that if you comment out a section of an R markdown document
using HTML comments, and that section contains an R code chunk, then that
code chunk is still executed when you knit the document. The execution is
silent, so any print statements/figures do not appear in the output.
However, if you modify variables in the chunk, they do get modified.

Note that if you "run all previous chunks in the source file" from a chunk
after the commented-out section, so that the code is executed in the
console, the commented-out chunk does not get executed. This is the
behavior I would expect to happen when you knit the document. Here's a
reproducible example:


title: "Bug when using HTML comments"
author: "Jonathan Gellar"
date: "June 8, 2016"

output: html_document

knitr::opts_chunk$set(echo = TRUE)

I am going to demonstrate a small bug that occurs when using HTML
comments. First, I'll generate some data:

x <- 1:5x

That was easy. Now I'm going to comment out a section using HTML comments.

Now we are done with the commented section. Let's print x and see what's
there:

xYou are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<https://github.com/rstudio/rmarkdown/issues/720>, or mute the thread
<https://github.com/notifications/unsubscribe/AAGXx6YYX_aYySBfGJBMa-3mvo5fDewcks5qJxklgaJpZM4IxUZu>
.

@jgellar
Copy link
Author

jgellar commented Jun 8, 2016

Yeah, I imagined this would be the case. I just think it would be more appropriate for the knitr parser to recognize that a code chunk is contained within an HTML comment, so the programmer probably does not intend for it to be executed. At least I would hope it would be consistent with what happens when you execute "Run all previous chunks in the source file", which recognizes that the chunk is commented out.

@jjallaire
Copy link
Member

Interesting, that's not an intended inconsistency!

@yihui, what do you think about respecting HTML comments in knitr?

On Wed, Jun 8, 2016 at 5:30 PM, jgellar notifications@github.com wrote:

Yeah, I imagined this would be the case. I just think it would be more
appropriate for the knitr parser to recognize that a code chunk is
contained within an HTML comment, so the programmer probably does not
intend for it to be executed. At least I would hope it would be consistent
with what happens when you execute "Run all previous chunks in the source
file", which recognizes that the chunk is commented out.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#720 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAGXxwWh39x7zNpLyxRXKoQ5H-oSrsTXks5qJzR1gaJpZM4IxUZu
.

@yihui
Copy link
Member

yihui commented Jun 9, 2016

As mentioned above, knitr does not know anything about HTML comments. There are two complications if I have to recognize HTML comments in knitr: 1) it is hard/impossible to do it well only by using regular expressions (on which knitr's parser is based); 2) I will also have to consider other document formats such as Rnw, Rhtml, and so on. So the only solution for now is eval=FALSE. Sorry.

@jgellar
Copy link
Author

jgellar commented Jun 9, 2016

Perhaps it could be done within rmarkdown? e.g., in rmarkdown::render, you add a step like

if(output_format == "html_document") {
    # search through the file for html comment tags, and remove everything between them
    # ....
}

Even if regular expressions are difficult for finding <!-- and --> (not sure why it would be, but I'll admit I don't know how to do this off the top of my head), there has to be some way to do it in R, right?

@yihui
Copy link
Member

yihui commented Jun 10, 2016

A simple example illustrating why it is difficult:

This is not an HTML comment `<!-- `

```{r}
```

`-->`

To reliably parse the HTML comment, I will have to use a real Markdown parser instead of regular expressions.

@yihui yihui added this to the v1.7 milestone Sep 1, 2017
@yihui yihui added enhancement RStudio IDE concerns the rstudio ide labels Sep 1, 2017
@yihui
Copy link
Member

yihui commented Oct 14, 2017

Closing this one and let's focus on #974 instead.

@yihui yihui closed this as completed Oct 14, 2017
@github-actions
Copy link

github-actions bot commented Nov 3, 2020

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
RStudio IDE concerns the rstudio ide
Projects
None yet
Development

No branches or pull requests

3 participants