Hi everyone, I want to write a function that modify directly variables passed as parameters (the equivalent in C language of *ptr/&ptr) so that I don't have to return a list and to reaffect all my variables. Is it possible to do so in R? Thanks a lot. Laetitia Marisa.
> I want to write a function that modify directly variables passed as > parameters (the equivalent in C language of *ptr/&ptr) so that I don't > have to return a list and to reaffect all my variables. Is it possible > to do so in R? Thanks a lot.You can use environments as they're passed by reference.
On Fri, 29 Aug 2003, Laetitia Marisa wrote:> I want to write a function that modify directly variables passed as > parameters (the equivalent in C language of *ptr/&ptr) so that I don't > have to return a list and to reaffect all my variables. > Is it possible to do so in R?Yes, with the .Call/.External interface (and there are examples in the ts package). To a limited extent it is possible with .C(DUP=FALSE), but you really don't want to go there. I would want a very good understanding of R's copying semantics before doing this (and those are liable to change, too). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
foo <- function(y) assign(deparse(substitute(y)), y^2, parent.frame()) assigns to the calling frame. Or you could put the required frame as a second argument.> -----Original Message----- > From: Barry Rowlingson [mailto:B.Rowlingson at lancaster.ac.uk] > Cutting to what I think was the gist of the original poster's > question, > can I write a function, foo, that does this: > > > x <- 3 > > foo(x) > > x > [1] 9 > > for any x, in any situation? I'm guessing its doable, but ugly... > > > Baz > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and\...{{dropped}}