[R] using object reference
Rajarshi Guha
rxg218 at psu.edu
Tue Mar 2 22:52:20 CET 2004
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