Syntax Highlighting for R scripts?

Posted on
Picture of user. neale Joined: 07/31/2009
Forums
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.

Replied on Wed, 02/29/2012 - 12:07
Picture of user. mdewey Joined: 01/21/2011

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.

Replied on Fri, 03/16/2012 - 11:16
Picture of user. neale Joined: 07/31/2009

In reply to by mdewey

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)
 
Replied on Fri, 03/16/2012 - 11:43
Picture of user. tbates Joined: 07/31/2009

In reply to by neale

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)
}
Replied on Mon, 03/26/2012 - 14:53
Picture of user. neale Joined: 07/31/2009

In reply to by AdminRoss

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.
Replied on Thu, 03/01/2012 - 11:02
Picture of user. tbates Joined: 07/31/2009

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/

Replied on Wed, 03/14/2012 - 16:33
Picture of user. tbates Joined: 07/31/2009

In reply to by tbates

seeing this everywhere lately...

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

Replied on Sun, 03/18/2012 - 11:44
Picture of user. tbates Joined: 07/31/2009

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

http://drupal.org/project/geshifilter