|
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? |
|
See packages R.oo and proto.
If you wish to do it yourself, you want to utilize environments for this. /Henrik On Thu, Jul 8, 2010 at 6:52 AM, Paul Bailey <[hidden email]> wrote: > > 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. > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
|
...and for tracing memory allocations/duplications, see tracemem().
/Henrik On Thu, Jul 8, 2010 at 9:14 AM, Henrik Bengtsson <[hidden email]> wrote: > See packages R.oo and proto. > > If you wish to do it yourself, you want to utilize environments for this. > > /Henrik > > On Thu, Jul 8, 2010 at 6:52 AM, Paul Bailey <[hidden email]> wrote: >> >> 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. >> >> ______________________________________________ >> [hidden email] mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
|
In reply to this post by Paul Bailey
On Thu, Jul 8, 2010 at 12:52 AM, Paul Bailey <[hidden email]> wrote:
> > 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) Try: g2 <- function(x, env = parent.frame()) { with(env, v1[[1]] <- v1[[1]] + v1[[2]]) } or g2 <- function(x, env = parent.frame()) { env$v1[[1]] <- env$v1[[1]] + env$v1[[2]] } Sometimes the reason people want pass by reference is not so much for efficiency is but that they are trying to recreate an object oriented structure without realizing it. In that case the packages referred to by Henrik would be useful. > > 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. > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
| Powered by Nabble | Edit this page |
