R syntax highlighting for bloggers on WordPress.com

Announcing the ability to highlight R syntax in WordPress.com blogs, thanks to the recent work of Yihui Xie, Yoav Farhi and Andrew Redd.

Good news for R bloggers who are using WordPress.com to host their blog.

This week, the good people running WordPress.com (special thanks goes to Yoav Farhi), have added the ability for all the users of the WordPress.com platform to be able to highlight their R code inside posts.

Basically you’ll need to wrap the code in your post like this:

[sourcecode language="r"]
test.function = function(r) {
    return(pi * r^2)
}
test.function(1)
[/sourcecode]

(Which will then look like this:
r syntax highlighted code example
)

Further details (and other supported languages) can be read about on this WordPress.com support page.

This new feature was possible thanks to the work of Yihui Xie (who create the famous cool animation package for R), who created a R syntax brush for the syntaxhighlighter WordPress plugin (the plugin used by WordPress.com for sytnax highlighting) . And thanks should also go to Andrew Redd, the creator of NppToR (which connects between notepad++ to R). He both made some good suggestions, and was game to take on the brush creation in case there would be problems, which thankfully so far there aren’t any)

p.s: If you are a WordPress.org users (e.g: have a self hosted WordPress blog) and want to enable R syntax highlighting for your blog, I would recommend the use of the WP-Syntax plugin (enhanced with GeSHi version 1.0.8.6) which can be downloaded here.

27 thoughts on “R syntax highlighting for bloggers on WordPress.com”

  1. Another way to accomplish this is to compose your blog post and apply syntax highlighting offline (using a package such as Pygments which has grammars for R source code and R console transcripts) and post to WordPress using the XMLRPC API. This just requires adding some CSS to your template. If anyone is interested, details are here: http://blog.dexy.it/241

    1. Ana –
      This is a nice hack, but it has three disadvantages:
      1) It is not free – adding CSS on WordPress.com costs money.
      2) It requires the user to work on their websites CSS (which requires more time and fiddling)
      3) The user will need to spend time preparing their R code.

      This looks (IMHO) less simple then simple copy-pasting the code and adding the proper tags around it.

      Cheers,
      Tal

        1. Hi Andrew, this could work on a self hosted site – but it won’t work on a WordPress.com hosted site (since they will not allow the css to function without paying them). Revolution tool is nice, although for practical blog writing purposes, I find that using the WordPress plugins is faster to use.

          Cheers,
          Tal

    1. WordPress.com is using the plugin “SyntaxHighlighter Evolved” to highlight the code (Tal told me this), and one ugly thing about any software is always on the documentation. I didn’t find any explanation on which tag to use in the webpages on that plugin, and in the end, I found them in the source code — anyone of ‘sourcecode’, ‘source’ and ‘code’ can be used (line 208: http://plugins.trac.wordpress.org/browser/syntaxhighlighter/trunk/syntaxhighlighter.php?rev=248650)

      I simply use <pre> because that is the safest way — you code won’t be affected much even if your highlighter tool fails.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.