How to include R plots and diagrams in blog posts

R plots and diagrams in blog posts

Published on September 14, 2015

R plots in Markdown

Using my R-pandoc tool, you will be able to embed nice R plots into blog posts, like the one below! It uses the fantastic pandoc document converter.

require(stats)
D = 150
T = 10
t = seq(0, 80, 0.01)
x = -D*exp(-(t/T))+D
v = (D/T)*exp(-(t/T))
plot(t, x, type="l", main="Evolution of position through time", xlab="time (s)", ylab="position (m)", xlim=c(0,80), ylim=c(0, D+10),  xaxs = "i", yaxs = "i")

The above code and diagram are produced by inserting this code block inside a markdown file:

    ``` {.Rplot echo=Above}
    require(stats)
    D = 150
    T = 10
    t = seq(0, 80, 0.01)
    x = -D*exp(-(t/T))+D
    v = (D/T)*exp(-(t/T))
    plot(t, x, type="l", main="Evolution of position through time", xlab="time (s)", ylab="position (m)", xlim=c(0,80), ylim=c(0, D+10),  xaxs = "i", yaxs = "i")
    ```

The attribute echo can also get the value Below to get the code displayed below the graphic. Ignore echo if you want only the graphic. Now you can compile your markdown blog post:

$ pandoc -t html demo.md --filter R-pandoc -o demo.html -s

A HTML file should be generated containing a nice graph.

Diagrams in Markdown

Using diagrams-pandoc (available soon), you can also produce nice diagrams inserted in Markdown.

example = square 1 # fc aqua `atop` circle 1

The diagrams above was obtained from the following code:

    ``` {.diagram}
    example = square 1 # fc aqua `atop` circle 1
    ```

It supports the same options than R-pandoc.

Embed in a Hakyll blog

To embed this in a Hakyll blog, you need to add R-pandoc in the dependencies of you project cabal file, and then define a special markdown compiler:

import Text.Pandoc.R

pandocCompilerR :: Compiler (Item String)
pandocCompilerR = pandocCompilerWithTransformM defaultHakyllReaderOptions defaultHakyllWriterOptions rTransformer

rTransformer :: Pandoc -> Compiler Pandoc
rTransformer pandoc = unsafeCompiler $ renderRPandoc "images" True pandoc

This compiler can be used instead the default one:

buildRmd :: Rules ()
buildRmd = do
    match "*.md" $ do
      route idRoute
      compile $ pandocCompilerR

Combining R-pandoc and diagrams-pandoc

What is really great is that transformers can be composed. To render an article containing both R plots and diagrams in command line, you can do it like that:

$ pandoc demo.md -t json | R-pandoc | diagrams-pandoc | pandoc -f json -t html

To do the same automatically with Hakyll, you can create a markdown compiler from R-pandoc and diagrams-pandoc with the famous fish >=> operator:

myPandocCompiler :: Compiler (Item String)
myPandocCompiler = pandocCompilerWithTransformM readerOptions writerOptions $ diagramsTransformer >=> rTransformer

This blog post has of course been generated with Hakyll, R-pandoc and diagrams-pandoc. Check out the source code here. If you want to learn more about pandoc filters, check here.

Comments