[R] passing parameters by reference?

Ben Bolker ben at eno.Princeton.EDU
Sun Mar 7 23:42:08 CET 1999


  I was translating some code from C to R and found that I would really
like to be able to pass some parameters to an R function by reference
(i.e., have the function make changes in the variables that then propagate
back to the calling environment).
  I have some vague knowledge of the whole assign(substitute(deparse()))
framework, but I was just wondering if there is some simpler/more accepted
idiom ...

  To make this concrete, suppose I want to write a swap() function that
swaps the values of two variables:

swap <- function(a,b) {
   tmp <- a; a <- b; b <- tmp
}

  clearly doesn't work.
  Is there something simple and bulletproof that would work?

  (Just a little more context: I'm translating some Numerical Recipes
optimization code to R for a class I'm teaching.  I could do it with a
call to C code, but I'd like the students to be able to play around with
the code easily.  For the time being I've worked around this problem, but 
it left me wondering.)

 thanks,
   Ben Bolker

Benjamin Bolker          Dep't of Ecology and Evolutionary Biology,
ben at eno.princeton.edu    Princeton University
tel: (609) 258-6886,     fax: (609) 258-1334

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list