{"id":183,"date":"2018-02-02T21:51:46","date_gmt":"2018-02-02T21:51:46","guid":{"rendered":"https:\/\/eipsoftware.com\/musings\/?p=183"},"modified":"2021-10-02T21:50:55","modified_gmt":"2021-10-02T21:50:55","slug":"r-markdown-to-wordpress","status":"publish","type":"post","link":"https:\/\/eipsoftware.com\/musings\/r-markdown-to-wordpress\/","title":{"rendered":"R Markdown to WordPress"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Posting an Rmd File to WordPress<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Updated: 2021-10-02 for R version 4.1<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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?<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">The required packages:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>&nbsp;<\/th><th>Package Name<\/th><th>Authors<\/th><th>Website<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Knitr<\/td><td>William K. Morris, Yihui Xie, and Jared Lander<\/td><td><a href=\"https:\/\/yihui.name\/knitr\/demo\/wordpress\/\">https:\/\/yihui.name\/knitr\/demo\/wordpress\/<\/a><\/td><\/tr><tr><td>2<\/td><td>RWordPress<\/td><td>Duncan Temple Lang<\/td><td><a href=\"https:\/\/github.com\/duncantl\/RWordPress\">https:\/\/github.com\/duncantl\/RWordPress<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Install the required packages.<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8217;t work. So I just copied the .zip files and changed them to .tar.gz and all was well with the world.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Linux: Install libcurl<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">~$ sudo apt install libcurl4-openssl-dev<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">R: Dependency packages<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Inside of R Studio, from the tools menu, Install Packages, Install the following packages, bitops, curl, XML, RCurl and xmlrpc2.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatively you can use the R command.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:r decode:true \"># NOTE: The package names are case sensitive\ninstall.packages(\"bitops\")\ninstall.packages(\"curl\") \ninstall.packages(\"XML\")\ninstall.packages(\"RCurl\")\n\n# if you don't want to use the XMLRPC package you can use the xmlrpc2\n# package but you will need to edit the RWordpress set up files to point\n# to the new package \ninstall.packages(\"xmlrpc2\")<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">If you don&#8217;t want to use the <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Code to install knitr package.<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># Install knitr, if you haven't already done so\nif (!require('knitr')) \n    {\n        install.packages(\"knitr\")\n    }\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Get the RWordPress package from <a href=\"https:\/\/github.com\">https:\/\/github.com<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are using Microsoft Windows you can use this link<br><a href=\"https:\/\/api.github.com\/repos\/duncantl\/RWordPress\/zipball\/master\">https:\/\/api.github.com\/repos\/duncantl\/RWordPress\/zipball\/master<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are using Linux or Linux Variant you can use this link<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/api.github.com\/repos\/duncantl\/RWordPress\/tarball\/master\">https:\/\/api.github.com\/repos\/duncantl\/XMLRPC\/tarball\/master<\/a><br><a href=\"https:\/\/api.github.com\/repos\/duncantl\/RWordPress\/tarball\/master\">https:\/\/api.github.com\/repos\/duncantl\/RWordPress\/tarball\/master<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Save the file to your local computer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inside of R Studio, from the tools menu, Install Packages. And from the pop up dialog box choose Install From <em>Package Archive File<\/em><br>And then browse for where you stored the local file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatively you can use the R command.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:r decode:true \"># edit the path to where you saved the file from github.\ninstall.packages('\/path\/to\/file\/duncantl-XMLRPC.tar.gz', repos = NULL, type=\"source\")\ninstall.packages('\/path\/to\/file\/duncantl-RWordPress.tar.gz', repos = NULL, type=\"source\")<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Sending the Converted Rmd File to WordPress<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After you have written and saved your Rmd file, you can then post it your WordPress site.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The code is mainly self-explanatory but a few notes.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>You need to make sure your local directory is set so that knitr can find the Rmd files.<\/li><li>Note the spelling in the options. It is case-sensitive and it is a lowercase p in WordPress.<\/li><li>knit2wp function from knitr is a wrapper around the functions in the RWordPress package. Use the ever useful ?knit2wp for details.<\/li><li>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.<\/li><li>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.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:r decode:true \"># make sure your local directory is set\nsetwd(\"~\/Documents\/local_rmd_files\")\n\n# make sure the neccessary libraries are loaded.\nlibrary(RWordPress)\nlibrary(knitr)\n\n\n# Put the WordPress username \/ password and URL for your WordPress site.\noptions(WordPressLogin = c(Your_UserName = 'YourPasswordtoWordPress'),\n        WordPressURL = 'https:\/\/www.yourwebsite.com\/wordpress\/xmlrpc.php')\n\n# knitr parses the Rmd file and pushes the html to your WordPress site\n# ?knit2wp for details\nknit2wp('test_rmd_to_wp.Rmd'\n        , title = 'Blog Posting from R Markdown to WordPress'\n        , action = \"newPost\"\n        ,publish = FALSE)<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Checking the Results<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can login to your WordPress website and check out how things look.<br>They should look fine, and if they do, go ahead and publish.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Feedback<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Let me know if you found the above useful.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"mailto:michael.data@eipsoftware.com\">michael.data@eipsoftware.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[4,5,7],"tags":[30,35,39],"series":[],"class_list":["post-183","post","type-post","status-publish","format-standard","hentry","category-code","category-r","category-visualization","tag-code","tag-r","tag-visualization"],"_links":{"self":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/comments?post=183"}],"version-history":[{"count":6,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"predecessor-version":[{"id":390,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts\/183\/revisions\/390"}],"wp:attachment":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/tags?post=183"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/series?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}