Posting an Rmd File to WordPress
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:
|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.
Code to install knitr package.
# Install knitr, if you haven't already done so
Get the RWordPress package from https://github.com
If you are using Microsoft Windows you can use this link
If you are using Linux or Linux Variant you can use this link
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.
# edit the path to where you saved the file from github.
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.
# make sure your local directory is set
# make sure the neccessary libraries are loaded.
# 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
, 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.
Let me know if you found the above useful.