[R] R as a programming language

Alexy Khrabrov deliverable at gmail.com
Wed Nov 7 13:46:06 CET 2007


Greetings -- coming from Python/Ruby perspective, I'm wondering about  
certain features of R as a programming language.

Say I have a huge table t of the form

run     ord     unit    words   new
1       1       6939    1013    641
1       2       275     1001    518
1       3       3314    1008    488
1       4       14154   1018    463
1       5       2982    1006    421

Alternatively, it may have a part column in front.  For each run (in  
a part if present), I select ord and new columns as x and y and plot  
their functions in various ways.  t is huge.  So I want to select the  
subset to plot, as follows:

t.xy <- function(t,part=NA,run=NA) {
	if (is.na(run)) {
		# TODO does this entail a full copy -- or how do we do references  
in R?
		r <- t
	} else if (is.na(part)) {
		r <- t[t$run == run,]
	} else { # part present too
		r <- t[t$part == part & t$run == run,]
	}
	x <- r$ord
	y <- r$new
	xy.coords(x,y)
}

What I'm wondering about is whether r <-t will copy the complete t,  
and how do I minimize copying in R.  I heard it's a functional  
language -- is there lazy evaluation in place here?

Additionally, tried to use --args command line arguments, and found a  
way only due to David Brahm -- who helped with several important R  
points (thanks Dave!):

#!/bin/sh
# graph a fertility run
tail --lines=+4 "$0" | R --vanilla --slave --args $*; exit
args <- commandArgs()[-(1:4)]
...

And, still no option processing as in GNU long options, or python or  
ruby's optparse.

What's the semantics of parameter passing -- by value or by reference?

Is there anything less ugly than

print(paste("x=",x,"y=",y))

-- for routine printing?  Can [1] be eliminated from such simple  
printing?  What about formatted printing?

Is there a way to assign all of

a <- args[1]
b <- args[2]
c <- args[3]

in one fell swoop, a lá Python's

a,b,c = args

What's the simplest way to check whether a filename ends in ".rda"?

Will ask more as I go programming...

(Will someone here please write an O'Reilly's "Programming in R"?  :)

Cheers,
Alexy


More information about the R-help mailing list