Syntax Highlighting for R scripts?

9 replies [Last post]
neale's picture
Offline
Joined: 07/31/2009

When looking at a script, or script snippet, I find it very helpful to see syntax highlighting. I am an addict of highlighting. However, it is difficult to get demo scripts, or script snippets highlighted. One possible solution might be this:

http://alexgorbatchev.com/SyntaxHighlighter/

which currently lacks R support (and its wish list forum has been closed due to spam) but it is customizable. Possibly, building an R highlighter - and particularly one that understands OpenMx functions - would be useful? Or perhaps someone has another solution?

I am thinking that syntax highlighting in the documentation as well as in demo, models/nightly etc might help - as long as one can still cut & paste or download the scripts for use. I wouldn't want that broken.

tbates's picture
Offline
Joined: 07/31/2009
GeSHi

Seems like under Drupal, we want to install the GeSHi plugin

http://drupal.org/project/geshifilter

tbates's picture
Offline
Joined: 07/31/2009
great idea mike.
tbates's picture
Offline
Joined: 07/31/2009
jealous
mdewey's picture
Offline
Joined: 01/21/2011
Kate does this

Assuming you have the R syntax highlighting available then if you export it as HTML it keeps the highlighting. I have not tested this exhaustively and your mileage may vary.

I assume other editors do this too.

neale's picture
Offline
Joined: 07/31/2009
Good point that!

However, I am struggling with getting Textmate to do this happily. With bundles-textmate-create html from selection I only get the following which lacks syntax highlighting (it's lit nicely in the editor itself). So this was why I was looking for a more central OpenMx solution, which would simplify html-tagging, e.g.,

library(OpenMx)

which would have the (possible) advantage of working consistently for users of different editors, systems etc. Textmate formatted code pasted below:

<span class="source source_r"><span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> Load Libraries
</span>require(<span class="variable variable_other variable_other_r">OpenMx</span>)
require(<span class="variable variable_other variable_other_r">statmod</span>)
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> -----------------------------------------------------------------------------
</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> Prepare Data
</span><span class="variable variable_other variable_other_r">depData</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">&lt;-</span> read.table(<span class="variable variable_parameter variable_parameter_r">file</span><span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">=</span><span class="string string_quoted string_quoted_single string_quoted_single_r"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_r">'</span>depData.txt<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_r">'</span></span>,<span class="variable variable_parameter variable_parameter_r">header</span><span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">=</span><span class="constant constant_language constant_language_r">TRUE</span>)
<span class="variable variable_other variable_other_r">selvars</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">&lt;-</span> names(<span class="variable variable_other variable_other_r">depData</span>)[c(<span class="constant constant_numeric constant_numeric_r">10</span><span class="keyword keyword_other keyword_other_r">:</span><span class="constant constant_numeric constant_numeric_r">19</span>)]
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> -----------------------------------------------------------------------------
</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> Set up function for analysis
</span><span class="meta meta_function meta_function_r"><span class="entity entity_name entity_name_function entity_name_function_r">nonNormTest</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">&lt;-</span> <span class="keyword keyword_control keyword_control_r">function</span></span> (<span class="variable variable_other variable_other_r">data</span>,<span class="variable variable_parameter variable_parameter_r">nQuadPoints</span><span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">=</span><span class="constant constant_numeric constant_numeric_r">10</span>,<span class="variable variable_parameter variable_parameter_r">nFactors</span><span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">=</span><span class="constant constant_numeric constant_numeric_r">1</span>) 
{                                                   
    <span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> Number of: items; quadrature points; maximum # of thresholds; and factors (1 only at present)
</span>    <span class="variable variable_other variable_other_r">nItems</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">&lt;-</span> dim(<span class="variable variable_other variable_other_r">data</span>)[[<span class="constant constant_numeric constant_numeric_r">2</span>]]
    <span class="variable variable_other variable_other_r">nthresh</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">&lt;-</span> vector(<span class="variable variable_parameter variable_parameter_r">mode</span><span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">=</span><span class="string string_quoted string_quoted_double string_quoted_double_r"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_r">"</span>integer<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_r">"</span></span>,<span class="variable variable_other variable_other_r">nItems</span>)
 </span>

tbates's picture
Offline
Joined: 07/31/2009
yes, to get it highlighted in tm you need css

<span class="source source_r">library(<span class="variable variable_other variable_other_r">OpenMx</span>)
 
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> which would have the (possible) advantage of working consistently
<span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> for users of different editors, systems etc. Textmate formatted code pasted below:
</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> Load Libraries
</span>require(<span class="variable variable_other variable_other_r">OpenMx</span>)
require(<span class="variable variable_other variable_other_r">statmod</span>)
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> -----------------------------------------------------------------------------
</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> Prepare Data
</span><span class="variable variable_other variable_other_r">depData</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">&lt;-</span> read.table(<span class="variable variable_parameter variable_parameter_r">file</span><span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">=</span><span class="string string_quoted string_quoted_single string_quoted_single_r"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_r">'</span>depData.txt<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_r">'</span></span>,<span class="variable variable_parameter variable_parameter_r">header</span><span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">=</span><span class="constant constant_language constant_language_r">TRUE</span>)
<span class="variable variable_other variable_other_r">selvars</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">&lt;-</span> names(<span class="variable variable_other variable_other_r">depData</span>)[c(<span class="constant constant_numeric constant_numeric_r">10</span><span class="keyword keyword_other keyword_other_r">:</span><span class="constant constant_numeric constant_numeric_r">19</span>)]
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> -----------------------------------------------------------------------------
</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> Set up function for analysis
</span><span class="meta meta_function meta_function_r"><span class="entity entity_name entity_name_function entity_name_function_r">nonNormTest</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">&lt;-</span> <span class="keyword keyword_control keyword_control_r">function</span></span> (<span class="variable variable_other variable_other_r">data</span>,<span class="variable variable_parameter variable_parameter_r">nQuadPoints</span><span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">=</span><span class="constant constant_numeric constant_numeric_r">10</span>,<span class="variable variable_parameter variable_parameter_r">nFactors</span><span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">=</span><span class="constant constant_numeric constant_numeric_r">1</span>) <span class="meta meta_block meta_block_r"><span class="punctuation punctuation_section punctuation_section_block punctuation_section_block_begin punctuation_section_block_begin_r">{</span>                                                   
<span class="punctuation punctuation_whitespace punctuation_whitespace_comment punctuation_whitespace_comment_leading punctuation_whitespace_comment_leading_r">    </span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_r"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_r">#</span> Number of: items; quadrature points; maximum # of thresholds; and factors (1 only at present)
</span>    <span class="variable variable_other variable_other_r">nItems</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">&lt;-</span> dim(<span class="variable variable_other variable_other_r">data</span>)[[<span class="constant constant_numeric constant_numeric_r">2</span>]]
    <span class="variable variable_other variable_other_r">nthresh</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">&lt;-</span> vector(<span class="variable variable_parameter variable_parameter_r">mode</span><span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_r">=</span><span class="string string_quoted string_quoted_double string_quoted_double_r"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_r">"</span>integer<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_r">"</span></span>,<span class="variable variable_other variable_other_r">nItems</span>)
<span class="punctuation punctuation_section punctuation_section_block punctuation_section_block_end punctuation_section_block_end_r">}</span></span></span>

AdminRoss's picture
Offline
Joined: 03/07/2011
I've installed and enabled

I've installed and enabled the GeShi highlighter on the Wiki.

For example the R code fragment below is now highlighted.

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

neale's picture
Offline
Joined: 07/31/2009
Brilliant, thank you!

So to clarify, one has to use the tags code and /code with < and > around each to start and stop a code block.

mxOption(NULL, "Number of Threads", omxDetectCores() - 1)

I really like this, although I recognize that the ~6% red-green colorblind men and the small fractions of other colorblindness groups may not view this modification with rose-colored glasses.

rgore's picture
Offline
Joined: 01/27/2011
RE: Red/Green issues

Great point about the red/green contrast. We can probably fiddle with the defaults. I'll make some time to look into this week.