When I purl/tangle a document to extract the R chunks into a script, is there any way to:
Although this is more a trick than a solution, you can still modify the script resulting from purl with some regular expressions.
For example with the following function (maybe there is a simpler solution for the regex) :
dropchunks <- function(scriptname, what.to.drop){
script <- readLines(scriptname)
script <- do.call(paste, list(script, collapse = "\n") )
subpattern = paste0("(", do.call(paste, list(what.to.drop, collapse="|")), ")")
mainpattern <- paste('(?s)## @knitr ((?!## @knitr).)*?', subpattern, '.*?((?=## @knitr)|$)', sep="")
script <- gsub(pattern = mainpattern, replacement = "", x = script, perl=TRUE)
writeLines(text = script, con= scriptname)
}
You can then do this to remove all code chunk containing eval=F :
library(knitr)
purl("test.Rmd")
dropchunks("test.R", "eval=F")
You can do this to remove the chunk named "function" or "example" (and in fact it will remove any chunk that contains these words somewhere but this could be changed by changing the regex):
purl("test.Rmd")
dropchunks("test.R", c("function", "example"))