R syntax highlighting for bloggers on WordPress.com

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.

  • http://ananelson.com Ana Nelson

    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

    • http://www.talgalili.com Tal Galili

      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

      • http://ananelson.com Ana Nelson

        Thanks for pointing out the cost issue. I have now added support for a CSS-inliner so you don’t need to modify your site’s CSS.

      • http://bleachbit..sourceforge.net Andrew

        Today I saw this tool which converts R code to HTML+CSS which works with any web site

        http://www.inside-r.org/pretty-r/tool

        • http://www.talgalili.com Tal Galili

          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

  • http://decisionstats.wordpress.com/ Ajay Ohri

    Great collaborative work Tal, and thanks for suggesting this. R is an awesome community truly.

    btw I am on Wordpres.com so it helps!

  • http://www.bojanorama.pl michal

    Awesome! Thanks a lot to all involved!

    BTW, is there any difference between [sourcecode] and [code]? I was always using the latter.

    • http://yihui.name Yihui

      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.

  • http://yihui.name Yihui

    Tal, is my animation package really “famous”? :)

  • Pingback: Use R to Analyze Players for your Fantasy Hockey League « Brock's Data Adventure

  • http://ygc.cwsurf.de ygc

    really cool. I will use it instead of wp-codebox.

    • http://www.talgalili.com Tal Galili

      Hi ygc – for WordPress.org sites, I believe that wp-codebox is a better solution.

  • Pingback: R syntax highlighting in wordpress.com | Michael Scroggie

  • Pingback: highlight R syntax in wordpress using wp-codebox | YGC

  • Pingback: WP-CodeBox: A better R syntax highlighter plugin for WordPress | R-statistics blog

  • Pingback: Things I’ve Learned About WordPress.com | r4stats.com

  • Pingback: First post, and its a doozy! | Landscape Ecology 2.0

  • Pingback: Validating R-Backtesting frameworks against Metatrader 4 with 99% tick accuracy | xmphforex