missing chunk names in knitr tangled R code

198 views Asked by At

When using Stangle on a Sweave document, the output includes a comment that gives the chunk name. This is incredibly useful in a teaching setting to orient students in the code as they cut-and-paste. However, when I use knitr to generate the .R code from a .Rnw, the chunk names are not included (at least that I can see):

knit('example.Rnw',tangle=TRUE)

I could run Stangle on a document written for knitr except that there are incompatibilities in chunk arguments that preclude running Stangle without errors.

Is there a way to force knitr to produce comments in tangled R code that include chunk names (and/or numbers) like native Stangle?

1

There are 1 answers

0
Ben Bolker On BEST ANSWER

Thanks to @kohske (I had just about figured this out for myself ...)

purl() has a documentation argument, which is set to 1 by default (1=add chunk headers to code; 0 does nothing, 2 adds chunk headers as roxygen comments). If you want to stick to knit(.,tangle=TRUE) you just need to opts_knit$set(documentation=1) before tangling.