[R] using object reference
Spencer Graves
spencer.graves at pdf.com
Wed Mar 3 10:55:45 CET 2004
Yes, but use sparingly, because any use of "<<-", "assign", etc.,
from within a function to change something other than what appears in
the standard return from a function call generates "spaghetti code" that
is difficult to maintain. A month or a year from now, someone (the
developer or someone else) may have difficulty understanding how "z" got
created or changed.
Consider the following:
> x <- 1
>
> f <- function(z) {
+ attr(z,'a') <<- 'some new text'
+ }
>
> f(x)
>
> z
[1] 1
attr(,"a")
[1] "some new text"
> x
[1] 1
If you intend to create an object "z" this way using the function
"f", that's fine. However, if you subsequently forget, it can be very
confusing:
> z <- 3
> f(2)
> z
[1] 2
attr(,"a")
[1] "some new text"
Why is "z" no longer "3"? I didn't obviously "assign" to "z" the
results of f(2).
> z <- "x"
> g <- function(a){
+ w <- f(a)
+ w2 <- paste("2", w)
+ list(w=w, w2=w2)
+ }
> g(2)
$w
[1] "some new text"
$w2
[1] "2 some new text"
> z
[1] 2
attr(,"a")
[1] "some new text"
>
Moreover, the resulting code will not transfer to S-Plus 6.2.
> f(2)
Problem in f(2): "<<-" not allowed in replacements: see ?"<<-"
Use traceback() to see the call stack
I suggest you use the standard function return mechanism, and not
try to melt gracefully through the wall.
hope this helps. spencer graves
Gabor Grothendieck wrote:
>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.
>
>______________________________________________
>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