rmarkdown块中的条件`echo`(或eval或include) [英] Conditional `echo` (or eval or include) in rmarkdown chunks

查看:269
本文介绍了rmarkdown块中的条件`echo`(或eval或include)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Rmarkdown文档(pdf或html),其中包含一些有条件执行"的块.我想到的一个特殊情况是,我可能希望输出的输出更加详细和文档化,以供同事进行内部审核,而为外部消费者提供一个简短的版本.我可能不需要或不需要向客户端显示数据操作步骤,而只是关键图和表.我也不想制作两个单独的文档,也不必手动指示要显示什么.

I would like to create an Rmarkdown document (pdf or html) that has some chunks "executed" conditionally. The particular case I have in mind is that I might want a more verbose and documented version of the output for internal review by colleagues, and a shorter version for external consumers. I may not want or need to show data manipulation steps to a client, but just key graphs and tables. I also do not want to make two separate documents or have to manually indicate what to show or not.

是否可以在Rmd的开头设置一个开关来指示例如将运行所有块的verbose = T或切换echo = F(或include = F)的verbose = F?

Is there a way to set a switch at the beginning of the Rmd that indicates, e.g., verbose=T that will run all chunks or verbose=F that toggles echo=F (or include=F)?

谢谢.

推荐答案

knitr选项可以表示为R表达式.根据 knitr网页上的输出"文档:

knitr options can be stated as R expressions. Per the "output" documentation on the knitr webpage:

请注意,knitr中的所有选项都可以从R表达式中获取值,这带来了主要手册中引入的条件评估功能.简而言之,eval=dothis表示eval的实际值取自全局环境中名为dothis的变量;通过操作此变量,我们可以打开/关闭一批大块的评估.

Note all options in knitr can take values from R expressions, which brings the feature of conditional evaluation introduced in the main manual. In short, eval=dothis means the real value of eval is taken from a variable named dothis in the global environment; by manipulating this variable, we can turn on/off the evaluation of a batch of chunks.

换句话说,如果您编写一些块,如:

In other words if you write some chunks like:

```{r label}
doNextChunk <- as.logical(rbinom(1,1,.5))
```

```{r conditional, eval = doNextChunk}
"hello world!"
```

这篇关于rmarkdown块中的条件`echo`(或eval或include)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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