knitr:::knitr_code

[UPDATE 06/06/2019] From knitr v1.23, knit_code is an exported function and so can be accessed with double-colon (i.e. knitr::knit_code) instead of triple-colon.

Yihui Xie in his recent post wrote

There was only one thing upon which I hesitated when deciding whether I should give users the access. That is knitr:::knit_code. Here the triple-colon is obviously a danger sign. When you can even modify the content of a code chunk, I have no idea what can happen. Evil or creative? I’ll leave it to you to think about.

Yup I saw it: “modify the content of a code chunk”.

A few days before this I made a tutorial slide that incrementally reveals building a scatter plot for ggplot.

The slide was made using xaringan and the incremental reveal was made by copying and pasting the slide multiple times, deleting lines and then adding highlight to the right line. It did the job but this was far from ideal especially when I decided to change the order of the line so that theme_bw appears last.
I wanted to do something simliar to the implementation in ninjutsu.css where there is a “layout” slide that acts as a template for incremental reveals of cells. This makes the task easier by needing to make change only to one layout slide. This solution wasn’t obvious to me if it involved code chunks.

Yihui’s post set me off to look into knitr:::knit_code. Quick googling didn’t reveal too much documentation on it. It was when I came across this Stack Overflow by Steve Powell then did I get it how to use it.

Then I had a xaringan slide made with only one code chunk that contains the entire ggplot code.

Now Yihui writes “Evil or creative? I’ll leave it to you to think about.” Well… now that I know how knitr:::knit_code works, it’s giving me ideas.

comments powered by Disqus