Tools to tidy up R code

December 28, 2010

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

Last week we made an impassioned plea for attention to style in formatting R and SAS code.

While it’s always better to adopt a consistent style and use it whenever you write code, the reality is that sometimes formatting slips (or you end up repurposing code that others wrote. In those situations, the formatR package (due to Yihui Xie) is a useful tool to help tidy up code.

Imagine that the poorly formatted and somewhat obfuscated code from the last entry has been copied to the clipboard:

plotdens=function(x,y,mytitle, mylab){densx = density(x)
densy = density(y);plot(densx,main=mytitle,lwd=3,xlab=mylab,
yvals,col="gray")};mytitle=paste("Test of ages: D=",round(fOo$statistic,3),
" p=",round(fOo$p.value,2),sep="");plotdens(age[female==1],
age[female==0],mytitle=mytitle,mylab="age (in years)")

Running the commands:


will generate the following output:

ds = read.csv("")
fOo = ks.test(age[female == 1], age[female == 0], data = ds)
plotdens = function(x, y, mytitle, mylab) {
densx = density(x)
densy = density(y)
plot(densx, main = mytitle, lwd = 3, xlab = mylab, bty = "l")
lines(densy, lty = 2, col = 2, lwd = 3)
xvals = c(densx$x, rev(densy$x))
yvals = c(densx$y, rev(densy$y))
polygon(xvals, yvals, col = "gray")
mytitle = paste("Test of ages: D=", round(fOo$statistic, 3), " p=",
round(fOo$p.value, 2), sep = "")
plotdens(age[female == 1], age[female == 0], mytitle = mytitle,
mylab = "age (in years)")

The cleaned up code is much easier to parse, with minimal effort. Some quibbles: I’m not as fond of using 4 spaces of indentation. And I always worry what’ll happen if there are syntax errors (yipes!). But this is a useful tool to have in your box.

To leave a comment for the author, please follow the link and comment on their blog: SAS and R. 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.

Search R-bloggers


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)