2

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" ?

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
sunxd
  • 743
  • 1
  • 9
  • 24

1 Answers1

3

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
})
@
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • Hi, Yihui, thanks for the answer. Do I miss something, I just paste your code into the setup chunk of me and I have a chunk named <>=, but this chunk is still running when I run "run all" in Rstudio. Or do you mean , this only works when Rstudio knit the pdf? – sunxd Nov 16 '16 at 18:42