Highlight the R syntax on your (WordPress) blog using the wp-syntax plugin

Download link for WP-Syntax plugin (with GeSHi version 1.0.8.6)

In case you have a self hosted WordPress blog, and you wish to show your R code in it, how would you do it?

The simplest solution would be to just paste the code as plain text, which will look like this:

x <- rnorm(100, mean = 2, sd = 3)
plot(x, xlab = “index”, main = “Example code”)

But if you would like to help our readers orient themselves inside your code by giving different colors to different commands in the code (a.k.a: syntax highlighting). So it would like something like this:

1
2
x <- rnorm(100, mean = 2, sd = 3) # Creating a vector
plot(x, xlab = "index", main = "Example code") # Plotting it

How then would you do it?

Plugin Installation

The easiest way to do this inside a self hosted WordPress blog is by installing a plugin called WP-Syntax:

WP-Syntax provides clean syntax highlighting using GeSHi — supporting a wide range of popular languages (including R). It supports highlighting with or without line numbers and maintains formatting while copying snippets of code from the browser.

But there is a problem. The current WP-Syntax version is using an old version of GeSHi, and only the newer version (currently GeSHi version 1.0.8.6) includes support for R syntax. In order to solve this I patched the plugin and I encourage you to download (the fixed version of) WP-Syntax from here, which will allow you to highlight your R code.

Usage

After installing (and activating) the plugin, in order to add R code to your post you will need to:
1) Only work in HTML mode (not the Visual mode). Or else, the code you will paste will be messed up.
2) Put your code between the <pre> tag, like this:

(Note: make sure that you rewrite the ” – so it will work.)

<pre lang=”rsplus” line=”1″>
…Your R code here…
</pre>

Final note: R Syntax highlight in other ways

If you wish to have R syntax higlight inside an HTML file, I encourage you can have a look at the highlight package, by Romain Francois.

If you want to higlight your R syntax inside wordpress.com, here is a blog post by Erik Iverson showing how to do that using Emacs.

p.s: If you have a blog in which you write about R, please let me know about it in the comments (Or just join R-bloggers.com) – I’d love to follow you :-)

Update: Stephen Turner wrote about a syntax highlighting solution for R and blogger using github gist. And also mentioned there another solution for self hosted wordpress blogs, via J.D. Long: a Github Gist plugin for WordPress. Go publish code :)

Tags: , , , , , , , , , , , ,
Posted under: R, wordpress

8 Responses to “Highlight the R syntax on your (WordPress) blog using the wp-syntax plugin”

  1. Erik Iverson says:

    Thanks for mentioning my method. I want to let everyone know that my post not only describes how to highlight the R syntax, but to actually use R input to automatically generate and highlight the output, using Sweave.

  2. [...] päällä. Nyt R koodin syntaksikorostus on saatavilla wp-syntax pluginiin, kiitos siitä kuuluu Tal Galilille. Päivitin muutamia vanhjoa postauksia käyttämään R-tyylistä korostusta. [...]

  3. [...] patch is avaliable which includes R code highlights, something which isn’t available in the default WP-Syntax [...]

  4. kafka says:

    Thank you, it worked for me!

  5. apeescape says:

    This works fairly well for wordpress.com as a simple solution.
    [sourcecode language = "python"]
    code here
    [/sourcecode]

  6. [...] about R, so I really want to know how to highlight R syntax in WordPress, and then find a post here. The post is about how to do highlighting when you have a self hosted WordPress blog, but in the [...]


Leave a Reply