Trying to share some code ideas

logo

R Markdown to WordPress

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 NameAuthorsWebsite
1KnitrWilliam K. Morris, Yihui Xie, and Jared Landerhttps://yihui.name/knitr/demo/wordpress/
2RWordPressDuncan Temple Langhttps://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.

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.

If you don’t want to use the

Code to install knitr package.

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.

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.

  1. You need to make sure your local directory is set so that knitr can find the Rmd files.
  2. Note the spelling in the options. It is case-sensitive and it is a lowercase p in WordPress.
  3. knit2wp function from knitr is a wrapper around the functions in the RWordPress package. Use the ever useful ?knit2wp for details.
  4. 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.
  5. 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.

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.

michael.data@eipsoftware.com

Previous

Creating Tables in Oracle 12 PL/SQL

Next

Dropping Tables in Oracle PL/SQL

1 Comment

  1. 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.

Leave a Reply to cpan Cancel reply

Your email address will not be published. Required fields are marked *

Powered by WordPress & Theme by Anders Norén