In a couple of courses I’ve used RPubs and GitHub for publishing my R code and work. As I have a own website withWordPress I want to blog from R to my own blog instead only through sharing R code via RPubs and GitHub. After searching over the internet I find some usefull stuff to do this. So let’s try it myself en let this be my first blog post created from Rstudio to WordPress.
Setting up WordPress and R
What do we need for wordpress:
- Crayon Syntax Highlighter plugin for highlighting R code in a blog post.
- WP QuickLaTeX for nice math rendering using syntax.
And in R we need:
- knitr, a website is coded in HTML,
knitr
can convert your R code from a R Markdown file to HTML code, this package is already installed with RStudio. - RWordPress allows publish blog posts from R to WordPress.
Load the necessary packages in R
1
2
3
4
|
if
(
!
require
(
'RWordPress'
)
)
install
.
packages
(
'RWordPress'
,
repos
=
'http://www.omegahat.org/R'
,
type
=
'source'
)
library
(
RWordPress
)
library
(
knitr
)
|
Setting options for RWordPess
1
2
|
options
(
WordpressLogin
=
c
(
your_username
=
"your_password"
)
,
WordpressURL
=
"http://your.blog.com/xmlrpc.php"
)
|
Syntax highlighting with Crayon Syntax Highlighter
To make syntax highlighting work in WordPress with the Crayon Syntax Highlighter plugin. R code should be enclosed in WordPress-shortcode instead of the knitr html output default
1
|
<pre>
<code
class
=
"r"
>
...
</code>
</pre>
|
to
1
|
<pre
class
=
"lang:r decode:true"
>
...
</pre>
|
We can’t do that with the default knitr2wp function of knitr. knitr2wp
is only based on the syntax which is used forSyntaxHighlighter. I don’t want to update all my other code on my blog.
For the Crayon Syntax Highlighter I’ve found a request of allowing proper code highlight when using Crayon. Below the copied requested function for using it to post a blog to my WordPress site with Crayon Syntax Highlighter as code highlighter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
knit2wpCrayon
<
-
function
(
input
,
title
=
"A post from knitr"
,
.
.
.
,
action
=
c
(
"newPost"
,
"editPost"
,
"newPage"
)
,
postid
,
encoding
=
getOption
(
"encoding"
)
,
upload
=
FALSE
,
publish
=
FALSE
,
write
=
TRUE
)
{
out
<
-
knit
(
input
,
encoding
=
encoding
)
on
.
exit
(
unlink
(
out
)
)
con
<
-
file
(
out
,
encoding
=
encoding
)
on
.
exit
(
close
(
con
)
,
add
=
TRUE
)
content
<
-
knitr
::
:
native_encode
(
readLines
(
con
,
warn
=
FALSE
)
)
content
<
-
paste
(
content
,
collapse
=
"\n"
)
content
<
-
markdown
::
markdownToHTML
(
text
=
content
,
fragment
.
only
=
TRUE
)
content
<
-
gsub
(
"<pre><code class=\"([[:alpha:]]+)\">(.+?)</code></pre>"
,
"<pre class=\"lang:\\1 decode:true\">\\2</pre>"
,
content
)
content
=
knitr
::
:
native_encode
(
content
,
"UTF-8"
)
title
=
knitr
::
:
native_encode
(
title
,
"UTF-8"
)
if
(
write
)
{
writeLines
(
text
=
content
,
con
=
gsub
(
x
=
out
,
pattern
=
"\\.md$"
,
replacement
=
".html"
)
)
}
if
(
upload
)
{
action
=
match
.
arg
(
action
)
WPargs
=
list
(
content
=
list
(
description
=
content
,
title
=
title
,
.
.
.
)
,
publish
=
publish
)
if
(
action
==
"editPost"
)
WPargs
=
c
(
postid
=
postid
,
WPargs
)
do
.
call
(
"library"
,
list
(
package
=
"RWordPress"
,
character
.
only
=
TRUE
)
)
print
(
do
.
call
(
action
,
args
=
WPargs
)
)
}
}
|
After adding the function into R we can post our first blog on WordPress and that would be this one :).
Publishing the post to your blog
Note: Below code should run directly from the R shell (first create the above function and load the RWordPress library) after the R Markdown has created and has runs once. It will post a lots same drafts and ends with a error. I cannot figure out why, I think there is a loop and it wants to post itself to your blog. If you want to upload it run it directly from the R shell with the upload option to TRUE
.
1
2
3
4
|
knit2wpCrayon
(
"r2blog.Rmd"
,
title
=
"Create blog posts from RStudio to Wordpress"
,
categories
=
c
(
"R"
,
"Programming"
)
,
publish
=
FALSE
,
upload
=
TRUE
)
|
1
2
3
4
|
##
##
## processing file: r2blog.Rmd
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
##
|
|
|
0
%
|
|
.
.
.
.
.
.
.
|
11
%
## ordinary text without R code
##
##
|
|
.
.
.
.
.
.
.
.
.
.
.
.
.
.
|
22
%
## label: unnamed-chunk-5
##
|
|
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
|
33
%
## ordinary text without R code
##
##
|
|
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
|
44
%
## label: unnamed-chunk-6
##
|
|
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
|
56
%
## ordinary text without R code
##
##
|
|
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
|
67
%
## label: unnamed-chunk-7
##
|
|
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
|
78
%
## ordinary text without R code
##
##
|
|
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
|
89
%
## label: unnamed-chunk-8
##
|
|
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
|
100
%
## ordinary text without R code
|
1
2
3
4
5
6
|
## output file: r2blog.md
[
1
]
"1068"
attr
(
,
"class"
)
[
1
]
"WordpressPostId"
|
I set the publish option to FALSE
, so the post will stay in draft mode and I can fine tune some formats, tags, set a featured image and add on the top the shortcode for using syntax.
This code can also be find on my GitHub.