[Rd] pass-by-reference

From: Paul Bailey <pdbailey_at_umd.edu>
Date: Wed, 07 Jul 2010 21:52:29 -0700 (PDT)

I'm working with a large object that I want to modify slightly in a function. Pass-by-reference would make a lot of sense, but I don't know how to do it.

I've searched this archive and thought that I can do something like

f <- function(x) {
  v1 <- list(a=x,b=3)
  g(x)
  v1
}

g <- function(x) {
  frame <- parent.frame()
  assign("v1",list(a=x,b=x),frame)
}

f(4)
returns list(a=4,b=4)

but what if I wanted to make v1[[1]] = v1[[1]] + v1[[2]] without creating a copy of v1?

f2 <- function(x) {
  v1 <- list(a=x,b=3)
  g2(x)
  v1
}

g2 <- function(x) {
  frame <- parent.frame()
  v1 <- get("v1",envir=frame)
  v1[[1]] <- v1[[1]] + v1[[2]]
}

f2(4)

but this fails. (it returns list(a=4,b=3) because v1 was copied into g2, not passed by reference) Is there a way to do this?

-- 
View this message in context: http://r.789695.n4.nabble.com/pass-by-reference-tp2281802p2281802.html
Sent from the R devel mailing list archive at Nabble.com.

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Thu 08 Jul 2010 - 04:58:30 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 08 Jul 2010 - 13:20:13 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive