Dress your R code for the Web with Pretty R

November 4, 2010

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

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:

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) 

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



CRC R books series

Contact us if you wish to help support R-bloggers, and place your banner here.

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)