When I purl/tangle a document to extract the R chunks into a script, is there any way to:
The tangle processing block doesn't currently expand params, but we can make it do so...
# See `?trace` documentation for details.
bdy <- as.list( body( knitr:::process_tangle.block ) )
trace.at <- which( grepl(".*opts_chunk\\$merge.*",
as.list( body( knitr:::process_tangle.block ) ) ) )
tracer <- quote({
# Code borrowed from normal chunk procesing.
af = opts_knit$get('eval.after'); al = opts_knit$get('aliases')
if (!is.null(al) && !is.null(af)) af = c(af, names(al[af %in% al]))
for (o in setdiff(names(params), af)) params[o] = list(eval_lang(params[[o]]))
# Omit this if using lastest knitr source from github.
if( isFALSE( params$include ) ) {
tmp <- knit_code$get();
tmp[[params$label]] <- "";
knit_code$restore(tmp)
}
})
trace( knitr:::process_tangle.block, tracer=tracer, at=trace.at, print=FALSE )
Then purl() exclusion can be controlled using option arguments...
```{r setup, echo=TRUE, results='hide'}
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 setup2, ref.label="setup", echo=FALSE, results='markup'}
```
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, eval=!opts_knit$get("tangle") }
plus(1, 2)
```
And here's another example of its use:
```{r example2, include=!opts_knit$get("tangle") }
plus(3, 3)
```