Showing posts with label tidying code. Show all posts
Showing posts with label tidying code. Show all posts
Tuesday, December 28, 2010
Tools to tidy up R code
[フレーム]
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.
Labels:
programming style,
tidying code
2
comments
Subscribe to:
Comments (Atom)