9

I'm editing an R markdown file (.Rmd) that has a lot of R code blocks to move groups of those code blocks into "child" documents to simplify rearranging sections (if nothing else). As I convert sections to child documents, I would like to test the new child document without running the rest of the blocks and other children. However, when I use to comment out those sections, the R blocks still run (but RStudio makes the sections "look" as though they were commented out).

If I eliminate the preceding and trailing "```"s (i.e., the code block signifiers), the commenting works fine. However, as I said, I've got lots of code blocks and something like would be more convenient.

So, how do I comment out the R code blocks so they won't run?

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • 3
    I'm not sure if this is what you're looking for, but if you highlight from (at least) one row below an R code chunk to (at least) one row above the R code chunk, and then type Command-Shift-C, RStudio will place html comment tags on the chunk. – eipi10 Sep 05 '17 at 15:32
  • 1
    I should have mentioned: "Command-Shift-C" is for OSX. On Windows, I assume it's ctrl-shift-C (although this works on OSX as well). – eipi10 Sep 05 '17 at 15:39
  • That was what I was looking for! I was using the like it supported multiline comments (like \**...*\ in java), but that doesn't seem to be the case. However, the Command-Shift-C in RStudio (on OSX) turns the block into a set of single line comments--works perfectly. Thanks! – William Stockhausen - NOAA Fed Sep 05 '17 at 17:50
  • Since this resolved the issue, I've added it as an answer. – eipi10 Sep 05 '17 at 19:31

2 Answers2

18

In RStudio, if you highlight from (at least) one row above an R code chunk to (at least) the last row of the R code chunk,1 and then type ctrl-shift-C (in OSX or Windows) or command-shift-C (OSX only), RStudio will place html comment tags on the chunk.

For example:

```{r cars}
summary(cars)
plot(pressure)
```

After highlighting this and type ctrl-shift-C, this becomes:

<!-- ```{r cars} -->
<!-- summary(cars) -->
<!-- plot(pressure) -->
<!-- ``` -->

To selectively comment out multiple chunks, you can use the RStudio find/replace tool with the regex option checked. It takes two replacement steps (it can probably be done in one step, but I'm not sure how to do a regex to capture across multiple lines in RStudio).

Step 1: Comment out the first line of one or more chunks:

Find: (```{r.*)
Replace: <!--\1

Step 2: Comment out the last line of one or more chunks:

Find: (```)$
Replace: \1-->


1 You must include the row above the chunk in the highlight. Otherwise, RStudio will place R comment tags (#) at the beginning of each row of the chunk and the commented lines will appear as plain text in the output document.

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • I didn't realize this was a capability of R Studio. Every time I've tried to comment out a chunk, I've always gotten the `#`. It might help to stress in your answer that this _only_ works if you highlight at least row above the chunk. (In R Studio 1.0.143, highlighting from one above to the end of the chunk works, and highlighting from the start of the chunk to one row below the end give the `#` comment symbol). – Benjamin Sep 05 '17 at 19:43
  • You did. I guess I had in mind bolding that part of the sentence. – Benjamin Sep 05 '17 at 19:44
  • I like it. Thanks. – Benjamin Sep 05 '17 at 19:51
7

In an Rmarkdown document, we can apply certain options to each R code chunk that determines whether the code inside will be run, printed, show error messages, etc.

To have a specific code chunk not run, use:

```{r cars, eval=FALSE}
summary(cars)
```

To have a specific code chunk not run or print into the created doc, use:

```{r cars, eval=FALSE, echo=FALSE}
summary(cars)
```

"TRUE" is used for the opposite effects and is the default.

If you have many code chunks you need to comment out, you can take the suggestion from @eipi10 (thanks) and use find/replace with the regex option selected. So, the find would be "(```{r.*)", and the replace would be "\1, eval=FALSE, echo=FALSE}" (without the double quotes).

www
  • 4,124
  • 1
  • 11
  • 22
  • Thanks, but it was much faster to just comment out the blocks. – William Stockhausen - NOAA Fed Sep 05 '17 at 17:53
  • That would depend on how many consecutive blocks you're trying to comment out at one time, which you didn't specify. That aside, this answer is still the best solution for the average use case, for others that may be searching for this. – www Sep 05 '17 at 17:59
  • 3
    You can implement this approach more quickly using the `regex` option with the find/replace tool. The find pattern would be `(```{r.*)}` and the replace pattern would be `\1, eval=FALSE, echo=FALSE}`. – eipi10 Sep 05 '17 at 19:55