Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sweave to R markdown file conversion - code or converters available?

I am not sure if this type of question complies with the SO rules for well-defined questions ;) ... anyway:

I want to convert several R Sweave files (.Rnw) to R markdown files (.Rmd). Jeromy Anglin has posted on this matter here but there is no code supplied. I tried to use pandoc, but of course pandoc cannot handle the chunk tags and inline code tags correctly.

Consclusion: I guess I will have do write some code to parse my .Rnw files to prepare them for pandoc conversion. Thus my questions:

  1. Is there a better way to go?
  2. Does someone by chance have code available that will do the job?

TIA

like image 441
Mark Heckmann Avatar asked Jan 05 '14 13:01

Mark Heckmann


1 Answers

As @Karl commented, LaTeX --> markdown is not a trivial conversion as there are far more options and environments available in LaTeX compared to markdown. You are probably best off working with something like pandoc (see Demo #5). Basically, instead of doing

.Rnw --> .Rmd --> .md

you would do

.Rnw --> .tex --> .md

with pandoc. If you really want to go from .Rnw --> .Rmd, you may want to check out the pander package to write a function to extract code chunks, convert the remaining LaTeX content to markdown, and then re-insert the code chunks into the markdown document.

like image 51
mikeck Avatar answered Nov 17 '22 21:11

mikeck