Dress your R code for the Web with Pretty R

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

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

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)