如何在R markdown中注释掉R代码块? [英] how to comment out R code blocks in R markdown?

查看:614
本文介绍了如何在R markdown中注释掉R代码块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑一个R标记文件(.Rmd),该文件具有很多R代码块,可以将这些代码块组移动到子文档中,以简化重新排列节的内容(如果没有其他操作)。在将节转换为子文档时,我想在不运行其余块和其他子文档的情况下测试新的子文档。但是,当我用来注释掉那些部分时,R块仍然运行(但是RStudio使这些部分看起来像被注释掉一样看起来)。

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.

因此,如何注释掉R代码块,以便它们不会运行?

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

推荐答案

在RStudio中,如果您突出显示(至少)R代码上方的一行块(至少)到R代码块的最后一行 1 ,然后键入ctrl-shift-C(在OSX或Windows中)或command-shift-C(仅OSX) ),RStudio会将html注释标签放在该块上。

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.

例如:

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

在突出显示此内容并键入ctrl-shift-C之后,它变为:

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

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

要有选择地注释掉多个块,可以使用RStudio查找/替换工具并选中regex选项。 。它需要两个替换步骤(可能可以在一个步骤中完成,但是我不确定如何在RStudio中跨行捕获正则表达式。)

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).

步骤1:注释掉一个或多个块的第一行:

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


查找:(```{r 。*)

替换:<!-\1

步骤2:注释一个或多个块的最后一行:

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


查找:(```)$

替换: \1->






1您必须在突出显示部分中包含该行上方的行。否则,RStudio会将R注释标签()放在该块的每一行的开头,并且注释行将在输出文档中显示为纯文本。


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.

这篇关于如何在R markdown中注释掉R代码块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆