For teaching purposes, I would like to purl
the chunks of my .Rnw
file into separate files. This answer explains how to do it:
How to purl each chunks in .Rmd file to multiple .R files using Knitr
BUT the method does not preserve the chunk options. Since the chunks I have produce plots, it's important to preserve the fig.width
and fig.height
options. Ideally I would like a chunk that looks like this:
<<plot, fig.width = 3, fig.height = 5, outwidth = '.75\\textwidth'>>=
plot (1,1)
@
to become a file named plot.R
that looks like this:
#+ fig.width = 3, fig.height = 5
plot (1,1)
That is, turn the chunk options fig.width
and fig.height
into a format that will be recognized by spin()
, as purl()
does, and get rid of the chunk options that are irrelevant, or create problems for spin()
into Word, such as out.width
. All in the spirit of creating code files that are user-friendly.