[R] using object reference

Tom Blackwell tblackw at umich.edu
Wed Mar 3 00:19:19 CET 2004


Rajarshi  -

Read  help("<-")  to see what's going on in the example
from previous emails.  In your new function, in addition
to setting  attr(z,'a')  you will need to assign the
result in the global environment.

However, please think hard about whether the final result
couldn't be achieved in some much more straightforward
manner than setting an attribute on an existing object.
That sounds like asking for trouble down the road.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Tue, 2 Mar 2004, Rajarshi Guha wrote:

> 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.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list