If you have some R code to include in a document, especially a Web-based document like a blog post, the new "Pretty R" feature on inside-R.org can help you make it look its best. Given some raw R code, it will create a HTML version of the code, adding syntax highlighting elements and links. Functions, strings, comments and literals are all color-coded to make reading easier, and function names are linked to the corresponding R help pages in the Language Reference from inside-R.org.
For example, the Freakonometrics blog had an excellent post the other day on using time series methods to forecast pageviews reported on Google Analytics. (It's a great blog if you haven't checked it out before. I also like it because it gives me a chance to brush up on my French. But I digress.) The author, Arthur Charpentier, did include the R code, but I found it a little hard to read. Here's a screenshot:
I cut-and-pasted the code into the Pretty R tool (after removing the > and + prompts, which I recommend as good practice for posting R code -- it makes it easier for others to paste it into a script), and then just pasted the resulting HTML into this blog post. Here's the result:
base=read.table("http://perso.univ-rennes1.fr/arthur.charpentier/million2.csv", sep=";",header=TRUE) X2=cumsum(base$nombre) X=X1+X2 kt=which(D==as.Date("01/06/2010","%d/%m/%Y")) D0=as.Date("08/11/2008","%d/%m/%Y") D=D0+1:length(X1) P=rep(NA,(length(X)-kt)+1) for(h in 0:(length(X)-kt)){ model <- arima(X[1:(kt+h)],c(7 , # partie AR 1, # partie I 7),method="CSS") # partie MA forecast <- predict(model,200) u=max(D[1:kt+h])+1:300 k=which(u==as.Date("01/01/2011","%d/%m/%Y")) (P[h+1]=1-pnorm(1000000,forecast$pred[k],forecast$se[k])) }
The Pretty R tool is open to everyone, you don't need to set up an account to use it. I hope it's useful to all R authors and bloggers.
inside-R.org: Pretty R Syntax Highlighter
Personally I really do not like the underlines. And the highlight (http://www.andre-simon.de/) software has been there for ages, which supports R language as well. I used it a couple of years ago, but later I found it was not a good idea to use the hard-coded HTML to dress up R code (very difficult to maintain). Now I'm using SyntaxHighlighter, which can highlight R code (as well as other languages) dynamically using JavaScript -- we do not need to worry about possible future changes to our code. It seems Pretty R is using GeSHi.
A detailed explanation on how to use SyntaxHighlighter in any web pages to highlight R language: http://yihui.name/en/2010/09/syntaxhighlighter-brush-for-the-r-language/
Posted by: Yihui | November 04, 2010 at 11:03
Thanks for the comments Yihui, some useful links there. Pretty R is a web-based tool, so it's nice that there's no program you have to download, just fill out the form and your done. I don't use WordPress so I can't use SyntaxHighlighter, and I do find the fact that Pretty R links to documentation pretty useful. (You can control the appearance of the underlines with CSS if you want, too.)
Posted by: David Smith | November 04, 2010 at 11:22
I vote a big YES for the underlines! I'm using Pretty R formattted scripts in examples posted to my course web pages for my undergraduate statistics courses. The links are a painless way to get my students into the documentation. Thanks.
Posted by: Mike Anderson | November 04, 2010 at 17:39
Thanks for the tip (and for the compliment about my blog...). I have to check if dotclear likes it (this is the reason why I used this purple color for my R code... it was the only way to include R code, and to make clear that it was not some text),
Posted by: Arthur | November 05, 2010 at 04:09
To make it clear, SyntaxHighlighter has nothing to do with WordPress. It can be used in any HTML pages.
As I said, it is hard to maintain the code once it was converted to HTML. SyntaxHighlighter can keep the code "clean" and easy to maintain, because it uses JavaScript to highlight the content of the 'pre' tag. Anyway, if we are confident that our code is mature once written, Pretty-R can be a useful tool. In comparison, SyntaxHighlighter does not even require the Internet connection...
@Mike Anderson: Actually I mean the aesthetics, so my opinion is completely personal and can be biased. I guess underlines were heavily used quite a few years ago and they are rare now in modern web pages.
Posted by: Yihui | November 08, 2010 at 11:06
I really like this little tool :)
Though, if you're offline, if you use RKWard, you only have to do File -> Export as HTML... and you're done.
Posted by: moondowner | December 21, 2010 at 17:12