[R] using object reference

Gabor Grothendieck ggrothendieck at myway.com
Wed Mar 3 04:27:51 CET 2004


The double arrow in the example you cite is essential;
you can't leave it out since that's what tells R to 
search through its parents' environments:

attr(z,'a') <<- 'some new text' 


---
Date:   Tue, 02 Mar 2004 16:52:20 -0500 
From:   Rajarshi Guha <rxg218 at psu.edu>
To:   R <r-help at stat.math.ethz.ch> 
Subject:   [R] using object reference 

 
Hi,
I have read the previous thread on using references to objects in a
function but the solution suggested does'nt seem to be working.

basically I have an object x which has an attribute a containing some
text. I would like to pass x to a function which will change the
attribute a with some new text and have the change visible when the
function exits.

something like

attr(x,'a') <- 'some text'
f <- function(z) {
attr(z,'a') <- 'some new text'
}

So that when I call f(x)
attr(x,'a')

gives

'some new text'

I went by the example below 
g <- function(z) eval(eval(substitute(expression(z[1] <<- z[1]+1))))
a <- 1:5
g(a) # increments first element of a by 1
a # c(2,2,3,4,5)

replcing the innermost bracket with attr(z,'a') <- 'some new text' but
the the after returning from the function the attribute of x does not
get changed.

Could anybody point out how I could achieve this? Do I need to use the
R.oo package or can this be done without external packages?

Thanks,

-------------------------------------------------------------------
Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net>;
GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
-------------------------------------------------------------------
How I wish I were what I was when I wished I were what I am.




More information about the R-help mailing list