RMarkdown WordPress Template

Publish with R Markdown in WordPress

Run wordpress chunk with option eval=FALSE,echo=FALSE to publish this R Markdown file on your WordPress website. Eval is set to FALSE, otherwise the posting will be trapped in a loop.

# replace   make sure chunk option is set to echo=FALSE !
if (!require('RWordPress')){
  install.packages('RWordPress', repos = 'http://www.omegahat.org/R', type = 'source')
  if (!require('RWordPress')) {
  devtools::install_github(c("duncantl/XMLRPC", "duncantl/RWordPress"))
  }else{
   message('RWordPress installed Sucessfully !') 
  }
}

# Set login parameters (replace admin,password and blog_url!)
library(RWordPress)
password <- ''
options(WordPressLogin = c('' = password),
        WordPressURL = '/xmlrpc.php')

library(knitr)
# Knitr options: upload plots/images to wordpress
opts_knit$set(upload.fun = function(file){RWordPress::uploadFile(file)$url;})

# enable toc (comment out if not needed)
library(markdown)
options(markdown.HTML.options =  c(markdownHTMLOptions(default = T),"toc"))

# Upload featured image / post thumbnail: option: wp_post_thumbnail=postThumbnail$id
image_path <- ""
Rmd_path <- ""
postThumbnail <- RWordPress::uploadFile(,overwrite = TRUE)

postid <- knit2wp(Rmd_path,
                  action = c("newPost"),
                  title = 'RMarkdown WordPress Template',
                  categories=c('R'),
                  mt_keywords = c('R','RMarkdown'),
                  wp_post_thumbnail=postThumbnail$id,
                  publish=FALSE)# not to publish immediately

Plot

The following plot shows the annual blog frequencies and serves as example of a succesful file upload.

library(ggplot2)
ggplot(mtcars, aes(factor(cyl)))+geom_bar()

Blog posts by year

Math

Test the Math equation rendering:

Inline formula with \(a=3\) and \(b=5\)

\[ x = a + b \]

Table

a b
1 1.4146736
2 -0.2677549
3 -2.1880925
4 -0.2188177
5 1.0784949

Update existing post with R Markdown

# Get the post id from recent posts
posts <- getRecentPostTitles(num = 1, blogid = 0L, login = getOption("WordpressLogin",
  stop("need a login and password")))
postid <- as.character(posts_info[1,"postid"]) # assuming it's the last entry
# Get the post (to keep category,tags and title as before)
post <- getPost(postid=postid, login = getOption("WordpressLogin", 
  stop("need a login and password")))
# Edit the post and upload 
Rmd_path <- 'RMarkdown WordPress Template.Rmd'
knit2wp(Rmd_path,
        postid=postid,
        action = c("editPost"),
        title=post$title,
        categories=post$categories,
        mt_keywords=post$mt_keywords,
        wp_post_thumbnail=post$wp_post_thumbnail,
        publish=FALSE)

Upload files: csv

# create a testfile
d <- data.frame(a=1:10,b=rnorm(10))
write.table(d,file="dataset.csv",sep=";",row.names=FALSE,na = "",quote = FALSE)

# upload file to WordPress
library(RWordPress)
fileUrl <- RWordPress::uploadFile("dataset.csv")$url
Download: dataset

Upload files: PDF

# Provide a PDF of this article
library(rmarkdown)
Rmd_path <- 'RMarkdown WordPress Template.Rmd'
render(Rmd_path, "pdf_document")
pdfUrl <- RWordPress::uploadFile("PublishBlogPosts.pdf")$url

Enable Math equations and Code highlighting in WordPress

Further resources

blog for reference

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s

在WordPress.com寫網誌.

向上 ↑

%d 位部落客按了讚: