Create blog posts from RStudio to WordPress

 

In a couple of courses I’ve used RPubs and GitHub for publishing my R code and work. As I have a own website with WordPress I want to blog from R to my own blog instead only through sharing R code via RPubs and GitHub. After searching over the internet I find some useful stuff to do this. So let’s try it myself en let this be my first blog post created from Rstudio to WordPress.

Setting up WordPress, R and Rstudio

What do we need for wordpress:
And in R we need:
  • knitr, a website is coded in HTML, knitr can convert your R code from a R Markdown file to HTML code, this package is already installed with RStudio.
  • RWordPress allows publish blog posts from R to WordPress.
Load the necessary packages in R

Setting options for RWordPess

Syntax highlighting with Crayon Syntax Highlighter

To make syntax highlighting work in WordPress with the Crayon Syntax Highlighter plugin. R code should be enclosed in WordPress-shortcode instead of the knitr html output default

to

We can’t do that with the default knitr2wp function of knitr. knitr2wp is only based on the syntax which is used for SyntaxHighlighter. I don’t want to update all my other code on my blog.

For the Crayon Syntax Highlighter I’ve found a request of allowing proper code highlight when using Crayon. Below the copied requested function for using it to post a blog to my WordPress site with Crayon Syntax Highlighter as code highlighter.

After adding the function into R we can post our first blog on WordPress and that would be this one :).

Publishing the post to your blog

Note: Below code should run directly from the R shell (first create the above function and load the RWordPress library) after the R Markdown has created and has runs once. It will post a lots same drafts and ends with a error. I cannot figure out why, I think there is a loop and it wants to post itself to your blog. If you want to upload it run it directly from the R shell with the upload option to TRUE.

I set the publish option to FALSE, so the post will stay in draft mode and I can fine tune some formats, tags, set a featured image  and add on the top the shortcode for using L^AT_X syntax.

This code can also be find on my GitHub.

Related Posts