SyntaxHighlighter Brush for the R Language

[This article was first published on Statistics, R, Graphics and Fun » R Language, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Tal Galili requested in the R-help mailing list for a SyntaxHighlighter brush for the R language, so that WordPress users can highlight their R code easily. I promised to contribute a few minutes on this task, and here is the result:

shBrushR.js (1Kb)

 *  Author: Yihui Xie
 *  URL:
 *  License: GPL-2 | GPL-3
SyntaxHighlighter.brushes.R = function()
    var keywords = 'if else repeat while function for in next break TRUE FALSE NULL Inf NaN NA NA_integer_ NA_real_ NA_complex_ NA_character_';
    var constants = 'LETTERS letters pi';
    this.regexList = [
	{ regex: SyntaxHighlighter.regexLib.singleLinePerlComments,	css: 'comments' },
	{ regex: SyntaxHighlighter.regexLib.singleQuotedString,		css: 'string' },
	{ regex: SyntaxHighlighter.regexLib.doubleQuotedString,		css: 'string' },
	{ regex: new RegExp(this.getKeywords(keywords), 'gm'),		css: 'keyword' },
	{ regex: new RegExp(this.getKeywords(constants), 'gm'),		css: 'constants' },
	{ regex: /[\w._]+[ \t]*(?=\()/gm,				css: 'functions' },
SyntaxHighlighter.brushes.R.prototype	= new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.R.aliases	= ['r', 's', 'splus'];

Hopefully Tal can persuade the manager to add support for R syntax highlighting, so these users do not need to worry much about the installation and configuration. For WordPress users, there are a couple of choices to make use of SyntaxHighlighter, e.g. the SyntaxHighlighter Evolved plugin, but I find this plugin somehow out-dated because it is still using an old version of SyntaxHighlighter, and it looks not easy to add support for new languages. Therefore I decided not to use any WordPress plugins, but to manually add the necessary HTML code in my header and footer; this approach works for any HTML pages.

You need to upload the latest version (3.0.83) of SyntaxHighlighter somewhere first (of course, with the R brush shBrushR.js added in the scripts directory), change the following paths accordingly and insert them before the <body> tag in your HTML page:

<link rel="stylesheet" href="path/to/your/syntaxhighlighter/styles/shCore.css" type="text/css" />
<link rel="stylesheet" href="path/to/your/syntaxhighlighter/styles/shThemeDefault.css" type="text/css" />
<script type='text/javascript' src='path/to/your/syntaxhighlighter/scripts/shCore.js'></script>
<script type='text/javascript' src='path/to/your/syntaxhighlighter/scripts/shAutoloader.js'></script>

Then add these lines in the footer area (right before the </body> tag):

<script type="text/javascript">
 "r  path/to/your/syntaxhighlighter/scripts/shBrushR.js",
 "plain  path/to/your/syntaxhighlighter/scripts/shBrushPlain.js",
 "sql  path/to/your/syntaxhighlighter/scripts/shBrushSql.js",
 "js  path/to/your/syntaxhighlighter/scripts/shBrushJScript.js",
 "html xml  path/to/your/syntaxhighlighter/scripts/shBrushXml.js"
SyntaxHighlighter.defaults["toolbar"] = false;

Above is only my configuration; you may refer to the manual of SyntaxHighlighter for more information. To add those HTML code in your pages, you may modify the theme files (typically the header.php and footer.php). To make use of highlighting, you need to assign a special CSS class to your <pre></code> tag, e.g.</p> <pre><pre class="brush: r"> test.function = function(r) { return(pi * r^2) } test.function(1) </pre> </pre> <p>The result:</p> <pre>test.function = function(r) { return(pi * r^2) } test.function(1) </pre> <p>You may double-click on the code to select all of them, and copy them with normally (e.g. with Ctrl + C). The old version of SyntaxHighlighter uses a Flash file to copy the code to the clipboard and you have to select the line numbers as well when you only want to select the code; I do not like these features.</p> <p>Finally, the JS brush file can be improved in a few aspects but in fact I’m not quite good at JS RegExp, so I leave these possible improvements to the readers:</p> <ol> <li>highlight the function arguments — loosely speaking, the pattern is: they are in parentheses “()” followed by “=”; the first argument follows “(” and others follow “,”.</li> <li>highlight the variables — those strings followed by “<-” or “=” when “=” is not in paretheses.</li> </ol> <p>P.S. today I converted all my <code><pre></code> tags in this whole site to <code><pre class="brush: r"></code> using a SQL command:</p> <pre>UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`, '<pre>', '<pre>') WHERE `post_content` LIKE '%<pre>%'; </pre> <p>You may also consider this batch processing instead of manually adding <code>class="brush: r" to your


Related Posts

To leave a comment for the author, please follow the link and comment on their blog: Statistics, R, Graphics and Fun » R Language. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)