在rmarkdown/knitr中进行注释以防止R评估 [英] Commenting in rmarkdown/knitr to prevent R evaluation
问题描述
建议用于发表评论 .Rmd
文档使用HTML注释<!-在此处注释->
是不够的.我想注释掉文档的一部分,其中包括内联评估:
The recommendation for commenting in .Rmd
documents to use HTML commenting <!-- comment here -->
is insufficient. I'd like to comment out a section of my document that includes inline evaluations:
I haven't defined `x` yet.
<!-- So when I say that `x` is `r x` in the text, I'd like to comment it out -->
编织失败:
# |.................................................................| 100%
# inline R code fragments
#
#
#
#
# processing file: test.Rmd
# Quitting from lines 2-3 (test.Rmd)
# Error in eval(expr, envir, enclos) : object 'x' not found
# Calls: <Anonymous> ... in_dir -> inline_exec -> withVisible -> eval -> eval
# Execution halted
一种选择是对每个内联部分进行注释:
One option is to comment each inline section:
I haven't defined `x` yet.
So when I say that `x` is `r #x` in the text, I'd like to comment it out
但是,如果我想用几个这样的内联计算注释掉整个段落,就会遇到麻烦.有更规范的方法吗?
But this suffers if I'd like to comment out a whole paragraph with several such inline calculations, for example. Is there a more canonical way of doing this?
推荐答案
正如@NicE所说, knitr
首先要评估您的代码,尤其是因为可能会有内联R代码评估或其他R变量相关的文本,然后需要将其评估为markdown语法.例如,这包括在rmarkdown中:
As @NicE said, knitr
goes first to evaluate your code, in particular because there may be inline R code evaluation or other R variable dependent text, which then need to be evaluated as markdown syntax.
For instance, this included in rmarkdown:
Define if bold `r bold <- TRUE`
This text is `r ifelse(bold, "**bold**", "_italic_")`.
赠予:
定义是否为粗体
此文本为粗体.
然后,我认为插入注释而不对其进行评估的唯一方法是使用 eval = FALSE
&将其嵌入到块中 echo = FALSE
Then, I think the only way to insert comments without evaluating them is to embed them in a chunk with eval=FALSE
& echo=FALSE
```{r, eval=FALSE, echo=FALSE}
So when I say that `x` is `r x` in the text, I'd like to comment it out
```
这篇关于在rmarkdown/knitr中进行注释以防止R评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!