Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knitr and tangle code without execution

Tags:

r

knitr

I'm trying to convert a pretty critical database interface script to R markdown for documentation purposes, with the intent of then tangling that file to produce the R code that goes into my crontab. I've found however that if I set eval=T then kniting the file runs the code (which I don't want to happen unexpectedly) and if I set eval=F then tangling the file produces all commented code.

Is there a safe way to produce a file that tangles into runnable code without running the risk of execution? I suppose I could find/replace eval=F every time but that seems inelegant.

like image 705
Patrick McCarthy Avatar asked Apr 18 '26 22:04

Patrick McCarthy


1 Answers

Revisiting this question years later, there is a much easier answer:

You can combine the code chunk arguments eval=FALSE, comment=NA to:

  1. Avoid running the code chunk when running knit(), and
  2. Print code without comments when running purl()

This argument is described on the page: https://yihui.org/knitr/options/ in a section called "Code Decoration".

Perhaps this is already described in another post somewhere, but I just spent hours trying to figure out how to do this with "hooks" and knitr source code before finding the answer that I knew must be out there. Hope this makes it easier for the next person!

like image 157
Donal O'Leary Avatar answered Apr 20 '26 13:04

Donal O'Leary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!