How To Create A WordPress Blog From R Markdown?

Creating a WordPress blog directly from R Studio

The library packages that you would need for this purpose are:

  • RWordPress
  • knitr

library(RWordPress)
library(knitr)

You can download RWordPress  package from Git hub.

After you have the libraries loaded, you need to set up access to your wordpress site. For that, you will need:

  1. XML-RPC of your WordPress Blog
  2. Valid User Credentials
  3. Rmarkdown file to post

options(WordPressLogin = c(your_username = 'your_password'),
WordpressURL = 'www.sovitpoudel.com.np/xmlrpc.php')

filePath <- "D:\\R\\projects\\exploringClashRoyaleCards.Rmd"

knit2wp(filePath, title = 'Blogging directly from R using RWordPress', publish = FALSE)

The “knit2wp” function reads your markdown file and posts directly to your Blog a new blog post. You can configure whether you would like to publish the blog directly or not and also pass other parameters related to the post like keywords with this function.

You can upload texts, code blocks and outputs including the graphs with the help of this function setup.

Adding Colors and Syntax Highlights

By default, when we post WordPress Blog post directly from R Studio, the syntax highlights, and coloring are not compatible with WordPress Blog themes. We will need to manually provide reference to CSS and Javascript libraries for syntax highlighting codes.

We can achieve this by adding the following function in our functions.php file.


/* Add highlight.js support */
function highlight_load() {
wp_enqueue_style( 'highlight',
'//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css');
print "
<script src='//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js'>
</script>
<script src='//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/languages/r.min.js'>
</script>
<script>hljs.initHighlightingOnLoad();</script>";}
add_action('wp_footer','highlight_load');

References:

Tags: ,