首页 文章

knitr在HTML风格的注释中执行R代码块

提问于
浏览
1

使用 Rstudio 编辑 Rmarkdown 文件,我使用HTML注释标记出我不想处理的大块或在输出中 . 这在Rstudio中运行得很好,它忽略了注释中的所有内容 . 但是,当我要求Rstudio编织文档时, knitr 正在评论中执行 R 代码块 .

这是一个MWE .Rmd文件:

# Important stuff I want to see
This is what I want to see:
```{r}
pi # I like pi
<!--- **This section commented out, as not ready to be knit yet** This is what I do not want to see: ```{r} cake # I also like cake, but it's undefined ``` -->

这导致编织器失败 `Error in eval(expr, envir, enclos) : object 'cake' not found ... Execution halted` 

有没有一种简单的方法可以注释掉整个Rmarkdown文件,这会阻止knitr在评论中执行R代码块?

我查看了[global comment option for R markdown in knitr](https://stackoverflow.com/q/11190281/5568465)和[Comments in Markdown](https://stackoverflow.com/q/4823468/5568465),以及[https://yihui.name/knitr/](https://yihui.name/knitr/),但没有找到解决方案 .

3 回答

  • 0

    选择 {r} 代码块之前和之后的行,并使用Control / Shift-C(pc)注释掉 . 你会得到这种语法;它赢了't knit, and won' t给出错误 .

    <!-- **This section commented out, as not ready to be knit yet** -->
    <!-- This is what I do not want to see: -->
    <!-- ```{r} -->
    <!-- cake # I also like cake, but it's undefined -->
    <!-- ``` -->
    
  • 0

    考虑到jburkhardt使用 eval=F 的想法,这可以作为一种方法来执行块注释,其中knitr不执行R代码块:

    Stuff I want to see...
    ```{r}
    pi
    
    <!-- This is added at the beginning of the comment: ```{r, include=FALSE} knitr::opts_chunk$set(eval= FALSE) ``` Stuff I have commented out: ```{r} cake ``` This is added to the end of the comment: ```{r, include=FALSE, eval=TRUE} knitr::opts_chunk$set(eval= TRUE) ``` -->

    More stuff I want to see:

    2*pi
    
    
    它当然不是防弹的(例如,knitr仍将运行它使用显式 `eval=true` 找到的任何代码块),但它适用于我的情况 .
  • 1

    在第二个代码垃圾中,您可以设置 eval=F ,直到完成这部分代码 .

    <!--
    **This section commented out, as not ready to be knit yet**
    This is what I do not want to see:
    ```{r eval=F}
    cake # I also like cake, but it's undefined
    

    -->

相关问题