Write Posts With Rstudio, Rmarkdown Format And Publish Directly To Wordpress With Knitr & Rwordpress

本文介绍如何使用RStudio、R Markdown及knitr等工具撰写并自动发布到WordPress的完全开放、自由且可重现的文章。文章详细解释了从编写带有R代码的Markdown文档到最终在博客上发布的整个流程。

Introduction

Objective

chinaPleth is designed to be fully open, free and reproducible. This post will explain the tools and process we use to generate ou posts, weaving text and code together, publishing automatically to WordPress using :

  • Rstudio for code edition and execution in R
  • Rmarkdown to write posts and articles
  • knitr for processing (“weaving” text and code chunks together) Rmarkdown post
  • Rwordpress to publish directly Rmarkdown documents to a wordpress blog

But what does it mean exactly to be reproducible ? The most simple definition in our context is probably the one from the excellent book by Jeff Leek, The Elements of Data Analytic Style, A guide for people who want to analyze data (available here)

Reproducibility involves being able to recalculate the exact numbers in a data analysis using the code and raw data provided by the analyst.

Tools and R packages

The necessary software and packages needed are listed bellow. First we use (Rstudio)[http://www.rstudio.com] in its version 0.99.846 and R in its version R version 3.2.2 (2015-08-14). Then we need to load the following packages.

library(knitr) ## we use knitr 1.12
library(RWordPress) ## we use RwordPress 0.2-3
# check if we are in the right working directory
if(gsub("(.*)\\/", "", getwd()) != "Rmd") {setwd("./Rmd")}

Writing reports with R

Weaving text and code

The best way to understand how to produce fully reproducible reports and posts with R, Rmarkdown and knitr is to read the few examples given by yihui the developer of knitr here

Regardless of which format you use, the basic idea is the same: knitr extracts R code in the input document, evaluates it and writes the results to the output document. There are two types of R code: chunks (code as separate paragraphs) and inline R code

The process is the following :

  • write the document in Rmarkdown which is an evolution of markdown to embed R code (and executed code output to make them dynamic). You can read more about Rmarkdown here
  • “compile” or “weave” the document with knitr generating an html (or PDF or word) document which contains the initial text and the inline code and output of the R code (figures, table, etc…)

Rwordpress

Rwordpress is an additional package which generate a document in html for and publish it directly to your wordpress blog together with categories, tags and status (draft or published). It is also possible to update and existing post knowing it's ID.

The documentation regarding Rwordpress is quite scarce on the web and doesn't look updated, the best is to read the presentation from it's developper here

The RWordPress package allows one to publish blog posts from R to WordPress (see the newPost() function in the package). A blog post is essentially an HTML fragment, and knitr can create such a fragment from R Markdown with the markdown package. Below is how to do this with the function knit2wp() in knitr:

Our configuration

Writing post

We use the basic function of Rstudio, create one new *.Rmd file for each post with following header template. All posts are stored in a subfolder called Rmd, which is version controlled using git. You can find all source file in our github repository : https://github.com/longwei66/chinaPleth

To get an idea of the format, you can see bellow and example of header (the one of this post).

---
title: Write posts with Rstudio,  Rmarkdown format and publish directly to wordpress with knitr & Rwordpress
author: "chinaPleth"
date: "January 14, 2016"
output: html_document
---

Configuring Rwordpress

We host our own wordpress blog so we won't cover the specific issues to publish to wordpress.com blogging platform, there are other ressources available for that purpose.

We maintain another script file in R format which has the configuration of Rwordpress, login credential and log of posts publication. You must open and configure the xmlrpc feature in wordpress (see details here)

## Install RWordPress if missing
if (!require('RWordPress'))
        install.packages('RWordPress', repos = 'http://www.omegahat.org/R', type = 'source')

## Load the libraries
library(RWordPress)
library(knitr)

## Define the option to access chinaPleth.io
options(WordPressLogin = c(longwei = 'writeyourpasswordinclearhere'),
        WordPressURL = 'http://yourblog_xmlrpc_here')

Publishing to your blog

Once the previous steps are done, this is very easy to post to your blog.

knit2wp(
        input = 'your_post_file_in_Rmarkdown_format.Rmd', 
        title = 'Your post title as it will be shown in wordpress', 
        shortcode = FALSE, ## 
        publish = FALSE
        )

As stated by publish=FALSE the post will not be published and appear as draft in wordpress. You just need to change this to TRUE if you need to publish it directly.

Once publish if the odd of the chinese internet are with you and you are not burried in a timeout you will get in return the ID of your post. If you forget to note this information, you can get it directly from your wordpress dashboard.

Updating an existing post

If you need to update you post, you can just use the following code and change whatever is necessary, Rwordpress will overwrite previous post content and title.

knit2wp(
        input = 'your_post_file_in_Rmarkdown_format_v2.Rmd', 
        title = 'Your updated post title as it will be shown in wordpress', 
        shortcode = FALSE, ## 
        publish = TRUE,
        action = "editPost",
        postid = 102
        )

Categories and tags

If you want to add categories and tag to your post, this is very easy and can be done with few additionnal options to the knit2wp() script. When you update and existing post, the categories and tags information is replaced by the updated one.

knit2wp(
        input = 'your_post_file_in_Rmarkdown_format_v2.Rmd', 
        title = 'Your updated post title as it will be shown in wordpress', 
        shortcode = FALSE, ## 
        publish = TRUE,
        action = "editPost",
        postid = 102,
        categories=c('Reproducible research', 'r-cran'),
        mt_keywords=c('wordpress', 'knitr', 'Rmarkdown', 'Rwordpress')
        )

Code highlight

The default formatting of code chunks in HTML by knitr is to wrap code chuncks in <code class="r"> tags. This is not recognised nor formatted nicely per default by wordpress. There are several alternatives to get a clean code formatting with highlights. Either you follow recommentation of yihui to modify your blog headers and point to specific js scripts (see here or you use one of the numerous plugins of wordpress.
We chose the second option and installed :

  • Plugin Name: WP Code Highlight.js
  • Plugin URI: https://github.com/owt5008137/WP-Code-Highlight.js
  • Description: This is simple wordpress plugin for http://highlightjs.org library. Highlight.js highlights syntax in code examples on blogs, forums and in fact on any web pages. It's very easy to use because it works automatically: finds blocks of code, detects a language, highlights it.
  • Version: 0.5.7

About images

The default configuration of knitr will produce a standalone html file, it means the plot and images generated by R will be embedded directly in the html source code. This is nice for standard Rmd reports as they can be shared by email directly as a standalone file.
In a blog, this is not the best solution, if you want to build a gallery of your plots or if you want your reader to syndicate your blog as RSS.

If you would like to upload the images to your wordpress blog instead, you have to add the following line of code in a code chunck at the begining of your report (after the headers)

opts_knit$set(upload.fun = function(file){library(RWordPress);uploadFile(file)$url;})

Conclusion

This works !

All of this is working pretty well (otherwise you won't read this post)

Issues

But we still have some specific issues with posting through a proxy/VPN.

We have time to time to use a VPN from China to use google based packages such as ggmaps or Google Maps API through a proxy which is configured in our ~/.Renviron file the following lines

http_proxy=http://IP:port
https_proxy=https://IP:port

The latest version of Rwordpress do not work properlly and generates the following error.

Error in convertToR(xmlParse(node, asText = TRUE)) : 
  error in evaluating the argument 'node' in selecting a method for function 'convertToR': Error: 1: Opening and ending tag mismatch: META line 2 and head
2: Entity 'nbsp' not defined

We will need further investigation to fix that problem.

Further ideas / developement

Tags and categories

It's great to be able to add tags and categories to our posts, it would be even better to generate tags and category allocation automatically based on text mining of the post itself and past posts.
As we are mainly a R blog, the idea would be to extract from Rmd file :

  • the most used R functions for the code chuncks
  • the most relevant word for text parts

It's a nice project to work on which should be the subject of later posts.

Title

Another improvement should be the reuse automatically the Rmd post titel as defined in the Rmd header.

标题SpringBoot智能在线预约挂号系统研究AI更换标题第1章引言介绍智能在线预约挂号系统的研究背景、意义、国内外研究现状及论文创新点。1.1研究背景与意义阐述智能在线预约挂号系统对提升医疗服务效率的重要性。1.2国内外研究现状分析国内外智能在线预约挂号系统的研究与应用情况。1.3研究方法及创新点概述本文采用的技术路线、研究方法及主要创新点。第2章相关理论总结智能在线预约挂号系统相关理论,包括系统架构、开发技术等。2.1系统架构设计理论介绍系统架构设计的基本原则和常用方法。2.2SpringBoot开发框架理论阐述SpringBoot框架的特点、优势及其在系统开发中的应用。2.3数据库设计与管理理论介绍数据库设计原则、数据模型及数据库管理系统。2.4网络安全与数据保护理论讨论网络安全威胁、数据保护技术及其在系统中的应用。第3章SpringBoot智能在线预约挂号系统设计详细介绍系统的设计方案,包括功能模块划分、数据库设计等。3.1系统功能模块设计划分系统功能模块,如用户管理、挂号管理、医生排班等。3.2数据库设计与实现设计数据库表结构,确定字段类型、主键及外键关系。3.3用户界面设计设计用户友好的界面,提升用户体验。3.4系统安全设计阐述系统安全策略,包括用户认证、数据加密等。第4章系统实现与测试介绍系统的实现过程,包括编码、测试及优化等。4.1系统编码实现采用SpringBoot框架进行系统编码实现。4.2系统测试方法介绍系统测试的方法、步骤及测试用例设计。4.3系统性能测试与分析对系统进行性能测试,分析测试结果并提出优化建议。4.4系统优化与改进根据测试结果对系统进行优化和改进,提升系统性能。第5章研究结果呈现系统实现后的效果,包括功能实现、性能提升等。5.1系统功能实现效果展示系统各功能模块的实现效果,如挂号成功界面等。5.2系统性能提升效果对比优化前后的系统性能
在编写这样的代码时,我们需要利用某个平台或服务提供的API接口,例如在社交媒体、博客或其他内容分发平台上。这里我将以假设的Python和Discord API为例,因为Discord API允许用户自动化发送消息。请注意,实际操作可能需要遵守各平台的服务条款。 首先,请确保你已经有一个Discord开发者账号并创建了应用,获取到应用的`client_id`、`client_secret`以及`bot_token`。 ```python import discord from discord.ext import commands # 替换为你的实际配置 DISCORD_BOT_TOKEN = &quot;your_bot_token&quot; CHANNEL_ID_TO_POST = &quot;destination_channel_id&quot; class KeywordBot(commands.Bot): def __init__(self, **options): super().__init__(**options) self.http_session = discord.http.Session() async def on_message(self, message): # 过滤其他频道的消息 if not message.channel.id == CHANNEL_ID_TO_POST: return # 假设keyword_list是一个包含关键词的列表 keyword_list = [&quot;keyword1&quot;, &quot;keyword2&quot;] for keyword in keyword_list: if keyword in message.content.lower(): await message.channel.send(f&quot;Found a match: {message.content}&quot;) break # 使用token初始化 bot intents = discord.Intents.default() intents.typing = False # 避免等待用户输入确认 bot = KeywordBot(command_prefix=&#39;!&#39;, intents=intents) # 启动 bot @bot.event async def on_ready(): print(f&quot;{bot.user} is connected to Discord!&quot;) await bot.change_presence(activity=discord.Game(name=&quot;Monitoring keywords...&quot;)) bot.run(DISCORD_BOT_TOKEN) ``` 在这个示例中: - `on_message`事件监听器会检查每条新接收到的消息是否包含预定义的关键字。 - 如果匹配,它将把这条消息转发到个人频道。 **相关问题:** 1. 在什么场景下会使用这种代码? 2. 如何更改这个脚本来适应不同的API? 3. 如何保护代码不被滥用,比如频繁发送大量消息?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值