sunxd
sunxd

Reputation: 751

run selected code chunks in knitr by filtering names of code chunk

I am implementing an algorithm which has a lot of formulas in knitr. So I define some functions in some code chunks with names in the pattern <<Fun_bar>>=@, and define unit test in other code chunks with names in the pattern <<Test_foo>>=@. Now, I only want to run the function definition code chunks. Is there any functionality that could only execute those code chunks with names starting with "Test" ?

Upvotes: 2

Views: 520

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30174

You can set eval to TRUE dynamically for chunks with labels that match Test_ using option hooks, e.g.

<<setup, include=FALSE>>=
knitr::opts_hooks$set(eval = function(options) {
  options$eval = grepl('^Test_', options$label)
  options
})
@

Upvotes: 3

Related Questions