Sweave in Lyx to markdown in RStudio

RStudio is fast becoming the default environment for running R. The markdown option produces very nice output and is easy to use.

However Lyx is still is a good option when producing PDFs. The latest version now uses the KnitR package without any additional installation.


It would be nice if it were possible to export from Lyx directly as markdown files for RStudio. This may well be possible in the future. For the time being I have written a little shell script using sed that does most (not all) the work. There are still a few brackets to be tidied by hand, and any options on code chunks need setting again, but it makes the job much quicker. You should compare the two versions before compiling.

Available from GitHub here

sed -e 's_<<.*>>=_```{r}_g' -e 's_\@_```_g'  -e 's_@_```_g' -e 's_\\title{_# _g' -e 's_\\section{_## _g' -e 's_\\subsection{_### _g' -e 's_\\subsubsection{_#### _g' -e 's_\\includegraphics.*{/_![alt text](_g' -e 's_\\item_* _g' $1

Paste the code into a file (I called mine to_markdown),  make it executable and put it in your path. Then simply writing

to_markdown test.Rnw > output.Rmd

Will do the job quite nicely. You will still need to go through the output carefully by hand before knitting, but most of the changes will be straitforward.  You may need to set the width of the latex output in Lyx wider than the default before exporting as Rnw

Then turn into this ..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s