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:
)
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)
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.
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
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
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.
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
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
Great collaborative work Tal, and thanks for suggesting this. R is an awesome community truly.
btw I am on Wordpres.com so it helps!
Awesome! Thanks a lot to all involved!
BTW, is there any difference between [sourcecode] and [code]? I was always using the latter.
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.
Tal, is my animation package really “famous”? ๐
Yihui you are right, I exaggerated a bit. I’ll correct my post.
For my defense, you did come up in the R journal:
https://cran.r-project.org/doc/Rnews/Rnews_2008-2.pdf
And gave a talk in useR2010:
http://yihui.name/en/2010/07/my-talk-on-animations-at-user-2010/
๐
Don’t be so modest. Of course it is famous. As a matter of fact it rocks!
haha, thanks for the compliment — I really don’t have any idea on its impact.
really cool. I will use it instead of wp-codebox.
Hi ygc – for WordPress.org sites, I believe that wp-codebox is a better solution.
Thanks, I was struggling to include a R code in wordpress.
I had a few problems using this. I posted a screenshot at https://wordpress.com/post/outofdataerror.wordpress.com/682
It’s a shame it didn’T work for me. It would be so great if it did.