new R package : highlight
By romain francois on Sunday, November 22 2009, 10:01 - highlight - Permalink
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
Comments
Thanks, Romain! That's really fantastic!
Thanks so much, Romain! Now my reports look beautiful!
when i tried to install xtern256, it said "Warning: dependency ‘highlight’ is not available". Then i tried to install highlight, but it said 'Warning: invalid package .../highlight_0.1-4.tar.gz’.
Can you send me the full message, as well as the R version you are using, etc ...
Romain
Thanks for the code Romain,
I just used it on a post on my blog.
But I realized that in order for me to use the code, I first need to export the CSS to the local style.css of my blog, and then have the code itself inside the post.
Which leads me to offer to suggestions/requests:
1) To have an option to output the CSS outside the html.
2) To have an option to output a FULL CSS file, so one could add the entire file to his own blog, and after which to not bother with the CSS each time one is publishing his code.
Thanks again, I love your coding!
Cheers,
Tal
Thanks. That seems a reasonnable option. I think I would still like the default to inline the css though.
Very cool - been looking for something like this. Thanks!