RMarkdown to WordPress

One option is to create your markdown document in R, using \(\LaTeX\), knit to html, then copy the file content into WordPress and add at the top the following javascript code:

<script type="text/javascript"
  src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

This will load images into the blog post itself and preserve the \(\LaTeX\) formatting. I prefer to use the MathJax-LaTeX plugin, and use the MathJax CDN Service, without having to add the javascript to every post.


Use the latex2exppackage to translate expression to \(\LaTeX\) form, that can be added to plots, eg. axes labels or equations in notes.

library (latex2exp)
library(ggplot2)
ggplot(mtcars) + geom_jitter(aes(x=disp, y=qsec)) + ylab(TeX("\\frac{a^e}{b_x}")) + xlab(TeX("\\prod_{x}^{y}"))
latex2exp_supported(plot = T)

What does NOT work:

Have tried direct export but it does not work. It uses RWordpress, and logs in directly. Still needs the above javascript to preserve \(latex\) formatting, and needs additional gymnastics with the plot images. Some additional discussion here.

Math

Test the Math equation rendering: Inline formula with \(a=3\) and \(b=5\) \[x = a + b\]

Table

knitr::kable(data.frame(a=1:5,b=rnorm(5)))
ab
10.2091424
2-0.8850710
31.3184975
40.4808798
5-2.1093072

The easiest way to post your R markdown directly to WordPressis following Tobias Dienlin‘s method (or many others).

Create a separate publish.R file that will upload your post:

if (!require('knitr')) {install.packages("knitr")}
if (!require('devtools')) {install.packages("devtools")}
if (!require('RWordPress')) {devtools::install_github(c("duncantl/XMLRPC", "duncantl/RWordPress"))}

library(knitr); library(RWordPress)

options(WordPressLogin = c(user = 'PASSWORD'),
        WordPressURL = 'http://YOURWEBSITE.com/xmlrpc.php')

# Optional: If you want to see all of the knit2wp arguments that are possible, run this line.
?knit2wp

# If need be, set your working directory to the location where you stored the Rmd file. 
setwd("C:/Users/user/Documents")

knit2wp('post.RmD',
        title = 'YOUR TITLE',
        publish = FALSE,
        action = "newPost")

Leave a Reply