Skip to content

call by reference

2 messages · biter bilen, Gabor Grothendieck

#
a = 9
    b = 8
    print (a)
    print (b)
    fa<-function(bS, aV) {
            aV <<- 3
            assign(bS,3,env = .GlobalEnv)
    }
    fa("b", a)
    print (a) #9
    print (b) #3 #only this call is ok



----- Original Message ----
From: Prof Brian Ripley <ripley at stats.ox.ac.uk>
To: biter bilen <biterbilen at yahoo.com>
Cc: r-help at stat.math.ethz.ch
Sent: Wednesday, December 20, 2006 11:04:08 AM
Subject: Re: [R] call by reference

Can you tell us what you want to do with 'pass/call by reference'?

If you want an R function to alter its argument then it is possible 
(KalmanLike is an example), but it should only be possible via C code.
And if you know enough to do that, you probably would not be asking (and 
definitely not be asking on R-help rather than R-devel).
On Tue, 19 Dec 2006, biter bilen wrote:

            
That's not a fair summary of the help entry for .Alias.
#
You can do this:


   fb <- function(bS, env = parent.frame()) {
        bS <- deparse(substitute(bS))
        assign(bS, 3, env = env)
   }
   fb(bS)
   bS
   rm(bS)
  # or corresponding to your code
  fb(bS, env = .GlobalEnv)

Of course when typed in both give the same result since the global environment
is the parent frame.

Please use <- and = consistently and properly space your source code.
On 12/21/06, biter bilen <biterbilen at yahoo.com> wrote: