knitr-从`purl(...)`中排除大块吗? [英] knitr - exclude chunks from `purl(...)`?
问题描述
当我整理/纠缠文档以将R块提取到脚本中时,有什么方法可以实现:
When I purl/tangle a document to extract the R chunks into a script, is there any way to:
- 排除任意块(按名称说)?
- 如果不是,则排除
eval=F
块(或者也许我可以定义块挂钩/选项include=F
)?
- exclude an arbitrary chunk (by name say)?
- if not, exclude a chunk if
eval=F
(or perhaps I can define a chunk hook/optioninclude=F
)?
例如,假设我有以下Rmd:
For example, suppose I have the following Rmd:
```{r setup, echo=F}
library(MASS)
```
First, we perform the setup (assume for some reason I need to evaluate `setup`
silently before I wish to display the chunk to the user, hence the repetition)
```{r setup, eval=F}
```
Here's the function I've been explaining:
```{r function}
plus <- function (a, b) a + b
```
And here's an example of its use:
```{r example}
plus(1, 2)
```
缠结的脚本如下所示:
## @knitr setup, echo=F
library(MASS)
## @knitr setup, eval=F
library(MASS)
## @knitr function
plus <- function (a, b) a + b
## @knitr example
plus(1, 2)
我有一个想法,因为我不是要评估特定的块,因此至少它们不应出现在输出中(在上面的示例中,第二个setup
块).
I have the idea that since I didn't want particular chunks to be evaluated, they at the very least should not appear in the output (in the example above, the second setup
chunk).
另外,对于那些纠结的输出,将一些块标记为不可见"对我来说很好.我不想在输出脚本中使用example
块(出于文档目的,它在Rmd中很好用,但是如果我想使用
Additionally it would be nice for me to mark some chunks as "invisible" with respect to the tangled output. I don't want the example
chunk in my output script (it was nice in the Rmd for purposes of documentation, but I want to be able to tangle the Rmd and then just source('myfile.r')
if I wish to use the plus
function, without having to worry about these extra examples executing. Currently I tangle the Rmd and then manually edit out the chunks I don't want out of the script, which seems against the principle of just writing the one Rmd which will provide both documentation and script without extra effort.)
推荐答案
由于 knitr
1.3,有一个新块选项purl = TRUE/FALSE
,它允许为purl()
包括/排除某些代码块.
Since knitr
1.3, there is a new chunk option purl = TRUE/FALSE
that allows one to include/exclude certain code chunks for purl()
.
```{r test, purl=FALSE}
library(MASS)
```
这篇关于knitr-从`purl(...)`中排除大块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!