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
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 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

Log in or register to post comments