Tools to tidy up R code

December 28, 2010
By

(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:

ds=read.csv("http://www.math.smith.edu/r/data/help.csv");attach(ds)
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)")
legend(50,.05,legend=c("Women","Men"),col=1:2,lty=1:2,lwd=2)

Running the commands:

library(formatR)
tidy.source()

will generate the following output:

ds = read.csv("http://www.math.smith.edu/r/data/help.csv")
attach(ds)
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 his blog: SAS and R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: 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.