# Romain Francois, Professional R Enthusiast

## Tag - highlight

Tuesday, December 7 2010

## highlight 0.2-5

I pushed highlight 0.2-5 on CRAN. This release improves the latex renderer and the sweave driver so that multiple lines character strings are properly rendered.

This example vignette shows it:

\documentclass[a4paper]{report}
\begin{document}

<<echo=FALSE,results=hide>>=
old.op <- options( prompt = " ", continue = " " )
@

<<>>=
require( inline )
require( Rcpp )
convolve <- cxxfunction(
signature( a = "numeric", b = "numeric" ), '
NumericVector xa(a); int n_xa = xa.size() ;
NumericVector xb(b); int n_xb = xb.size() ;
NumericVector xab(n_xa + n_xb - 1,0.0);

Range r( 0, n_xb-1 );
for(int i=0; i<n_xa; i++, r++){
xab[ r ] += noNA(xa[i]) * noNA(xb) ;
}
return xab ;
', plugin = "Rcpp" )
convolve( 1:4, 1:5 )
@

<<echo=FALSE,results=hide>>=
options( old.op )
@

\end{document}



Once processed with Sweave, e.g. :

require( highlight )
driver <- HighlightWeaveLatex(boxes = TRUE)
Sweave( 'test.Rnw', driver = driver )
texi2dvi( 'test.tex', pdf = TRUE )


we get this result, embedded below with google viewer:

See this question on stack overflow for the tip of using google documents to display pdf files

Saturday, July 31 2010

## highlight 0.2-2

I've released highlight 0.2-2 to CRAN. This release adds the possibility to control the font size of the latex code generated by sweave (when using the driver that is provided by highlight)

For example, this C++ code (using Rcpp) will be highlighted in tiny size.

<<lang=cpp,size=tiny>>=
double square( double x){
return x*x ;
}

SEXP foo( SEXP xx ){
NumericVector x(xx) ;
return sapply( x, square ) ;
}
@


This is something we had to do manually when preparing the slides for useR! 2010

Monday, May 31 2010

## highlight 0.2-0

I've released version 0.2-0 of highlight to CRAN

This version brings some more additions to the sweave driver that uses highlight to produce nice looking vignettes with color coded R chunks

The driver gains new arguments boxes, bg and border to control the appearance of the code chunks. When boxes is set to TRUE, the R code chunks are surrounded in boxes, and the arguments bg and border control the background color and the color of the box

Also, when the other highlight is available, the driver will also color code example code in any language that highlight supports. To use this, just surorund the code with <<lang=foo>> for the language foo. For example:

 <<lang=cpp>>=
int main(){
return 0 ;
}
@


will output the content of the code chunk as highlighted c++. The Rcpp-modules vignette in the next version of Rcpp uses both these new features. (see the vignette source in r-forge. The vignette is rendered into latex using :

require(highlight)
driver <- HighlightWeaveLatex(boxes = TRUE)
Sweave( 'Rcpp-modules.Rnw', driver = driver )


Saturday, May 29 2010

## highlight 0.1-9

The version 0.1-8 of highlight introduced a small bug in the latex renderer.

This is now fixed in version 0.1-9 and the latex renderer also gains an argument "minipage" which wraps the latex code in a minipage environment. I've used this to make this vignette for an upcoming feature of Rcpp

Friday, May 21 2010

## highlight 0.1-8

I've pushed version 0.1-8 of highlight to CRAN. highlight is a syntax highlighter for R that renders R source code into some markup language, the package ships html and latex renderers but is flexible enough to handle other formats. Syntax highlighting is based on information about the code gathered by a slightly modified version of the R parser, available in the separate parser package.

Internal code has been modified to take advantage of new features of Rcpp such as the DataFrame c++ class.

Since R 2.11.0, it is possible to install custom handlers to respond to http request (GET, POST, ...). highlight takes advantage of this and responds to urls with html syntax highlighted functions. So if the httpd port used by the dynamic help system is 9000 (hint: tools:::httpdPort) :

Saturday, February 13 2010

## highlight 0.1-5

I've pushed the version 0.1-5 of highlight to CRAN, it should be available in a couple of days.

This version fixes highlighting of code when one wants to display the prompt and the continue prompt. For example, this code :

rnorm(10,
mean = 5)

runif(5)


gets highlighted like this:

using this code:

> highlight( "/tmp/test.R", renderer=renderer_html(document=T), showPrompts = TRUE, output = "test.html" )


Under the hood, highlight now depends on Rcpp and uses some of the C++ classes of the new Rcpp API. See the get_highlighted_text function in the code.

Sunday, November 22 2009

## new R package : highlight

I finally pushed highlight to CRAN, which should be available in a few days. The package uses the information gathered by the parser package to perform syntax highlighting of R code

The main function of the package is highlight, which takes a number of argument including :

• file : the file in which the R code is
• output : some output connection or file name where to write the result (The default is standard output)
• renderer : a collection of function controlling how to render code into a given markup language

The package ships three functions that create such renderers

• renderer_html : renders in html/css
• renderer_latex: renders in latex
• renderer_verbatim: does nothing

And additionally, the xterm256 package defines a renderer that allows syntax highlighting directly in the console (if the console knows xterm 256 colors)

Let's assume we have this code file (/tmp/code.R)

f <- function( x){
x + rnorm(1)
}

g <- function(x){}
h <- function(x){}


Then we can syntax highlight it like this :

> highlight( "/tmp/code.R", renderer = renderer_html(), output = "/tmp/code.R.html" )
> highlight( "/tmp/code.R", renderer = renderer_latex(), output = "/tmp/code.R.latex" )


which makes these files : code.R.html and code.R.latex

The package also ships a sweave driver that can highlight code chunks in a sweave document, but I'll talk about this in another post