Posting an Rmd File to WordPress
Updated: 2021-10-02 for R version 4.1
There are four R packages you will need to be able to post an Rmd file to WordPress. Yes there are alternatives you can generate the HTML yourself and copy and paste things, but why would you want to do it?
The required packages:
Package Name | Authors | Website | |
---|---|---|---|
1 | Knitr | William K. Morris, Yihui Xie, and Jared Lander | https://yihui.name/knitr/demo/wordpress/ |
2 | RWordPress | Duncan Temple Lang | https://github.com/duncantl/RWordPress |
Install the required packages.
Ok, here is where I had to do an extra step. The Knitr and Devtools package is a normal installation from CRAN repository. However the XMLRPC and RWordPress packages need to be installed from github. The built-in function from R-Studio to install from github didn’t work. So I just copied the .zip files and changed them to .tar.gz and all was well with the world.
Linux: Install libcurl
On Linux you will need to install libcurl dev. Open a terminal and enter in following command. SUDO will prompt you for your admin password.
1 |
~$ sudo apt install libcurl4-openssl-dev |
R: Dependency packages
Inside of R Studio, from the tools menu, Install Packages, Install the following packages, bitops, curl, XML, RCurl and xmlrpc2.
Alternatively you can use the R command.
1 2 3 4 5 6 7 8 9 10 |
# NOTE: The package names are case sensitive install.packages("bitops") install.packages("curl") install.packages("XML") install.packages("RCurl") # if you don't want to use the XMLRPC package you can use the xmlrpc2 # package but you will need to edit the RWordpress set up files to point # to the new package install.packages("xmlrpc2") |
If you don’t want to use the
Code to install knitr package.
1 2 3 4 5 6 |
# Install knitr, if you haven't already done so if (!require('knitr')) { install.packages("knitr") } |
Get the RWordPress package from https://github.com
If you are using Microsoft Windows you can use this link
https://api.github.com/repos/duncantl/RWordPress/zipball/master
If you are using Linux or Linux Variant you can use this link
https://api.github.com/repos/duncantl/XMLRPC/tarball/master
https://api.github.com/repos/duncantl/RWordPress/tarball/master
Save the file to your local computer.
Inside of R Studio, from the tools menu, Install Packages. And from the pop up dialog box choose Install From Package Archive File
And then browse for where you stored the local file.
Alternatively you can use the R command.
1 2 3 |
# edit the path to where you saved the file from github. install.packages('/path/to/file/duncantl-XMLRPC.tar.gz', repos = NULL, type="source") install.packages('/path/to/file/duncantl-RWordPress.tar.gz', repos = NULL, type="source") |
Sending the Converted Rmd File to WordPress
After you have written and saved your Rmd file, you can then post it your WordPress site.
The code is mainly self-explanatory but a few notes.
- You need to make sure your local directory is set so that knitr can find the Rmd files.
- Note the spelling in the options. It is case-sensitive and it is a lowercase p in WordPress.
- knit2wp function from knitr is a wrapper around the functions in the RWordPress package. Use the ever useful ?knit2wp for details.
- The publish = FALSE option means that WordPress will not make the posting publically visible. If you set it to TRUE then the posting will be publically visible immediately. I think it is better to put to false and preview the posting first in WordPress before making it publically available.
- The knit2wp will send the .Rmd file over as a post by default but the action parameter does allow you to change it to a WordPress page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# make sure your local directory is set setwd("~/Documents/local_rmd_files") # make sure the neccessary libraries are loaded. library(RWordPress) library(knitr) # Put the WordPress username / password and URL for your WordPress site. options(WordpressLogin = c(Your_UserName = 'YourPasswordtoWordPress'), WordpressURL = 'https://www.yourwebsite.com/wordpress/xmlrpc.php') # knitr parses the Rmd file and pushes the html to your WordPress site # ?knit2wp for details knit2wp('test_rmd_to_wp.Rmd' , title = 'Blog Posting from R Markdown to WordPress' , action = "newPost" ,publish = FALSE) |
Checking the Results
Now you can login to your WordPress website and check out how things look.
They should look fine, and if they do, go ahead and publish.
Feedback
Let me know if you found the above useful.
cpan
I was looking for a way to publish my notes from R to wordpress when I came across your post. Looks like only downloading RWordPress would cause an error, as there’s another package needed.
devtools::install_github(c(“duncantl/XMLRPC”, “duncantl/RWordPress”))
This would do the job.