You are here

Syntax Highlighting for R scripts?

10 posts / 0 new
Last post
neale's picture
Offline
Joined: 07/31/2009 - 15:14
Syntax Highlighting for R scripts?

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.

mdewey's picture
Offline
Joined: 01/21/2011 - 13:24
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 - 15:14
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:

# Load Libraries
require(OpenMx)
require(statmod)
# -----------------------------------------------------------------------------

# Prepare Data
depData <- read.table(file='depData.txt',header=TRUE)
selvars <- names(depData)[c(10:19)]
# -----------------------------------------------------------------------------

# Set up function for analysis
nonNormTest <- function (data,nQuadPoints=10,nFactors=1) 
{                                                   
    # Number of: items; quadrature points; maximum # of thresholds; and factors (1 only at present)
    nItems <- dim(data)[[2]]
    nthresh <- vector(mode="integer",nItems)
 
tbates's picture
Offline
Joined: 07/31/2009 - 14:25
yes, to get it highlighted in tm you need css

/* Stylesheet generated from TextMate theme
*
* Tomorrow Night
* http://chriskempson.com
*
*/

/* Mostly to improve view within the TextMate HTML viewer */
body {
margin: 0;
padding: 0;
}

pre.textmate-source {
margin: 0;
padding: 0 0 0 2px;
font-family: Monaco, monospace;
font-size: 11px;
line-height: 1.3em;
word-wrap: break-word;
white-space: pre;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space: -o-pre-wrap;
}

pre.textmate-source.tomorrow_night {
color: #C5C8C6;
background-color: #1D1F21;
}

pre.textmate-source .linenum {
width: 75px;
padding: 0.1em 1em 0.2em 0;
color: #888;
background-color: #eee;
}
pre.textmate-source.tomorrow_night span {
padding-top: 0.2em;
padding-bottom: 0.1em;
}
pre.textmate-source.tomorrow_night ::selection {
background-color: #373B41;
}
/* Comment */
pre.textmate-source.tomorrow_night .comment {
color: #969896;
}

/* Foreground */
pre.textmate-source.tomorrow_night .keyword_operator_class, pre.textmate-source.tomorrow_night .constant_other, pre.textmate-source.tomorrow_night .source_php_embedded_line {
color: #CED1CF;
}

/* Variable, String Link, Regular Expression, Tag Name */
pre.textmate-source.tomorrow_night .variable, pre.textmate-source.tomorrow_night .support_other_variable, pre.textmate-source.tomorrow_night .string_other_link, pre.textmate-source.tomorrow_night .string_regexp, pre.textmate-source.tomorrow_night .entity_name_tag, pre.textmate-source.tomorrow_night .entity_other_attribute-name {
color: #CC6666;
}

/* Number, Constant, Function Argument, Tag Attribute, Embedded */
pre.textmate-source.tomorrow_night .constant_numeric, pre.textmate-source.tomorrow_night .constant_language, pre.textmate-source.tomorrow_night .support_constant, pre.textmate-source.tomorrow_night .constant_character, pre.textmate-source.tomorrow_night .variable_parameter, pre.textmate-source.tomorrow_night .punctuation_section_embedded, pre.textmate-source.tomorrow_night .keyword_other_unit {
color: #DE935F;
}

/* Class, Support */
pre.textmate-source.tomorrow_night .entity_name_class, pre.textmate-source.tomorrow_night .entity_name_type_class, pre.textmate-source.tomorrow_night .support_type, pre.textmate-source.tomorrow_night .support_class {
color: #F0C674;
}

/* String, Symbols, Inherited Class, Markup Heading */
pre.textmate-source.tomorrow_night .string, pre.textmate-source.tomorrow_night .constant_other_symbol, pre.textmate-source.tomorrow_night .entity_other_inherited-class, pre.textmate-source.tomorrow_night .markup_heading {
color: #B5BD68;
}

/* Operator, Misc */
pre.textmate-source.tomorrow_night .keyword_operator, pre.textmate-source.tomorrow_night .constant_other_color {
color: #8ABEB7;
}

/* Function, Special Method, Block Level */
pre.textmate-source.tomorrow_night .entity_name_function, pre.textmate-source.tomorrow_night .meta_function-call, pre.textmate-source.tomorrow_night .support_function, pre.textmate-source.tomorrow_night .keyword_other_special-method, pre.textmate-source.tomorrow_night .meta_block-level {
color: #81A2BE;
}

/* Keyword, Storage */
pre.textmate-source.tomorrow_night .keyword, pre.textmate-source.tomorrow_night .storage, pre.textmate-source.tomorrow_night .storage_type, pre.textmate-source.tomorrow_night .entity_name_tag_css {
color: #B294BB;
}

/* Invalid */
pre.textmate-source.tomorrow_night .invalid {
color: #CED2CF;
background-color: #DF5F5F;
}

/* Separator */
pre.textmate-source.tomorrow_night .meta_separator {
color: #CED2CF;
background-color: #82A3BF;
}

/* Deprecated */
pre.textmate-source.tomorrow_night .invalid_deprecated {
color: #CED2CF;
background-color: #B798BF;
}

library(OpenMx)

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

# Load Libraries
require(OpenMx)
require(statmod)
# -----------------------------------------------------------------------------

# Prepare Data
depData <- read.table(file='depData.txt',header=TRUE)
selvars <- names(depData)[c(10:19)]
# -----------------------------------------------------------------------------

# Set up function for analysis
nonNormTest <- function (data,nQuadPoints=10,nFactors=1) {                                                   
    # Number of: items; quadrature points; maximum # of thresholds; and factors (1 only at present)
    nItems <- dim(data)[[2]]
    nthresh <- vector(mode="integer",nItems)
}
AdminRoss's picture
Offline
Joined: 03/07/2011 - 13:01
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 - 15:14
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 - 16:48
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.

tbates's picture
Offline
Joined: 07/31/2009 - 14:25
great idea mike.

yes: let's do this here!

The code is on github
https://github.com/alexgorbatchev/SyntaxHighlighter

free hosted version as well
http://alexgorbatchev.com/SyntaxHighlighter/download/

Here's a "brush" for R

http://yihui.name/en/2010/09/syntaxhighlighter-brush-for-the-r-language/

here's an example of it in action
http://xianblog.wordpress.com/2012/02/29/abc-in-roma-r-lab-1/

tbates's picture
Offline
Joined: 07/31/2009 - 14:25
jealous

seeing this everywhere lately...

http://thebiobucket.blogspot.com/2012/03/r-function-to-read-data-from-google.html

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

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

http://drupal.org/project/geshifilter