Using Rmarkdown with knitr to compose WordPress posts

What I’d like to be able to do is use knitr on an R markdown (i.e. .Rmd) document so include text, code, code output, and images to make a nice looking WordPress post that I can compose and edit locally. And advantage to this is that I can use whatever editor I want for doing the composing (like Aquamacs), and that everything is contained in a single source document.

Workflow

I think the flow will be something like:

  1. Create the post in an .Rmd file locally in emacs.

  2. Use knitr with my local version of R to process the Rmd into either a proper md (Markdown) or an HTML that I can just upload (NOTE: the HTML option doesn’t really seem to work … will maybe need to look into this some more).

  3. Upload (or copy/paste) the md or HTML source into WordPress. The big question is what will happen with generated images, etc. With Markdown, I think the images are created in a directory that is dynamically linked to, but for the HTML there is maybe some way that images are embedded (it appears the embedding isn’t compatible with copying and pasting the HTML source produced by knitr — I guess the images will have to be uploaded manually).

Some code

Here is an example:

x <- seq(1, 1000)
y <- rnorm(length(x))
plot(x, y)

plot of chunk unnamed-chunk-1

Post script

Copying and pasting the .md source produced by knitr works pretty well, though the image links will be broken. To fix this, each image produced by knitr needs to be added to the media library, and then inserted into the post to get the proper path. E.g. for the test figure above, the path is https://codedocean.files.wordpress.com/2014/01/unnamed-chunk-1.png. The linking to the image can still be done in a “markdown” way by using something like:

![plot of chunk unnamed-chunk-1](https://codedocean.files.wordpress.com/2014/01/unnamed-chunk-1.png)
Advertisements

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