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:

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 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)