Kate
Kate

Reputation: 293

Using knit spin or stitch to interleave R into a template

This is probably documented somewhere, but I cannot find it ...

I have an .R file which can be used as a read_chunk() file and call from an .Rnw latex template or in a .Rmd file for primary review.

This design worked well for the first part of our project, but since the .R file will change, it's not 'reproducible research'.

Since I already have a 'template' with the named chunks referring to chunks in the read_chunk() file, is there any way to interleave the R into the .Rnw for posterity?

Upvotes: 1

Views: 405

Answers (1)

baptiste
baptiste

Reputation: 77106

You want the reverse operation of purl(), I believe. I don't know any such function, but technically it doesn't seem too hard; all the pieces are there:

chunks <- knitr:::knit_code$get()
for(k in names(chunks))
  cat(c(sprintf("<<%s>>=", k), chunks[[k]], "@\n"), sep = "\n")

would return all the chunks in a format suitable for inclusion in the Rnw file.

Upvotes: 2

Related Questions