2017-05-04 73 views
4

recommendation评论.Rmd文件使用HTML评论<!-- comment here -->是不够的。我要评论我的文档的一部分,包括内嵌的评价:评论rmarkdown/knitr以防R评估

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 

一个选择是每个评内嵌部分:

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 

但是,如果我想用几个这样的内联计算来评论整个段落,则会受到影响。有没有更具规范的方式来做到这一点?

+0

不完全确定我的理解,但如果问题是注释掉许多内联R语句,可能是关键字搜索并在文本编辑器中替换?像*选择paragaph *然后'Ctrl-f>找到“\'r”替换“\'r#”'。 – lmo

+0

@lmo我的意思是,当然。我想也许这是更多的功能要求然后....是否有任何理由'knitr'不承认代码嵌入在评论中,并跳过评估? – MichaelChirico

+0

这是一个很好的问题。我只是试了一下,看看你的意思。在这种情况下,评论被忽略是很奇怪的。 – lmo

回答

4

正如@NicE所说,knitr首先会评估您的代码,尤其是因为可能存在内联R代码评估或其他R变量相关文本,因此需要将其评估为降级语法。 例如,这包括在rmarkdown:

Define if bold `r bold <- TRUE` 
This text is `r ifelse(bold, "**bold**", "_italic_")`. 

给出:

定义,如果大胆
这个文本是大胆

然后,我想插入注释不进行评价,是将它们嵌入一大块与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 
``` 
0

我发现YAML块使好得多意见的唯一途径,

--- 
title: "Untitled" 
author: "baptiste" 
date: "10/21/2017" 
output: html_document 
--- 

I haven't defined `x` yet. 

--- 
# here's a comment 
# ```{r} 
# x = pi 
# ``` 
--- 

不幸的是,它似乎不适用于之前解析的内联r代码。