[R] (structured) programming style

Simon Fear Simon.Fear at synequanon.com
Fri Sep 12 12:21:25 CEST 2003


Ross, it seems to me that a "non-R-like" part of your code is hinted
at in

> I'm thinking of a situation like
> a <- array(0, dim=c(10000, 10))
> and then I modify a one row at a time.
> a[34,] <- newrow
> So if I write directly to a, I just overwrite the row.

My guess is you could be doing better with an apply()
to modify all the rows at once. Just one new copy of a.

I don't think there's a perfect answer to your main question, about 
controlling where the assignments are made. I like to use 
assign() with an explicit environment argument in this context, 
rather than <<-. Makes me feel that I'm in control. To be absolutely 
sure, you can combine this with get()
(otherwise in assign(x, fn(x), pos=1) it's the most local x
that gets used in calculating fn(x). x <<- fn(x) has the same potential
problem, as recently discussed on this list.)

In fact, in such unusual circumstances I use global variables without
any
sense of shame, and I name them global.x (etc) - names I would 
never use except when deliberately overriding normal lexical
scope. This is much easier and less error prone than clever
manipulation of the sys.frames() stack.  I'd rather have
clear code with global variables than "beautiful" code that I 
don't have the skill to maintain...
 

Simon Fear
Senior Statistician
Syne qua non Ltd
Tel: +44 (0) 1379 644449
Fax: +44 (0) 1379 644445
email: Simon.Fear at synequanon.com
web: http://www.synequanon.com
 
Number of attachments included with this message: 0
 
This message (and any associated files) is confidential and\...{{dropped}}




More information about the R-help mailing list