I knowf R functions ca be called passing some parameters. My first question is: how are parameters passed to R functions ? Browsing through R archives I found an answer confirming taht parameters can be passed to the called function by value. I wonder whether passing the parameter address is possible somehow to allow the function to actually change its value. My second question is about running R scripts with parameters. Something similar to the s/w device (argc, argv) which is used to run a C program with initial parameters. Is this possible at all ? Thank you very much. Maura tutti i telefonini TIM! [[alternative HTML version deleted]]
No you can not pass an address to a function. If you want to change the value of something, then return it as a value and assign it to the object so you can see what is going on. Must be coming from the "C" environment where such things are allowed and lead to a number of problems. If you really have your heart set to do it, look at the '<<-' operator (or 'assign'), but use it with care. To retrieve environment values, use "Sys.getenv". On Sat, Jan 3, 2009 at 1:32 PM, <mauede at alice.it> wrote:> I knowf R functions ca be called passing some parameters. > My first question is: how are parameters passed to R functions ? > Browsing through R archives I found an answer confirming taht parameters can > be passed to the called function by value. I wonder whether passing the parameter > address is possible somehow to allow the function to actually change its value. > > My second question is about running R scripts with parameters. Something similar to the > s/w device (argc, argv) which is used to run a C program with initial parameters. > Is this possible at all ? > > Thank you very much. > Maura > > > > tutti i telefonini TIM! > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
R's variable passing mechanism is not call by value, but a mixture of unevaluated arguments (like the obsolete Lisp FEXPR) and call-by-need. It is like FEXPR in that the function can capture the unevaluated argument (using 'substitute'). But it is like call-by-need in that normal use of the argument (without 'substitute') evaluates the expression the first time the variable is evaluated and caches the result as the value of the variable. If the argument's value is never evaluated, the argument is not evaluated. For example: (function(x){print(1);x;print(2);x;print(substitute(x));4})(print(3)) [1] 1 << prints 1 before evaluating argument [1] 3 << evaluates argument [1] 2 << after evaluating argument << x has already been evaluated; is not reevaluated print(3) << quoted argument [1] 4 << returns 4 You will sometimes here the terms call-by-name or lazy evaluation used to describe R's mechanism, but those mean quite different things in other programming languages, so it's best to avoid those terms. So it is in fact possible to 'capture' a variable and modify it: increment <- function(var) eval(substitute(var<-var+1),parent.frame()) x<-23 increment(x) x ==> 24 The 'substitute' operates in the function's own frame, so 'var' evaluates to the argument; but then the evaluation is specified to happen in the parent frame. That said, as a general rule, it is very poor practice to do this sort of thing. -s On Sat, Jan 3, 2009 at 1:32 PM, <mauede at alice.it> wrote:> I knowf R functions ca be called passing some parameters. > My first question is: how are parameters passed to R functions ? > Browsing through R archives I found an answer confirming taht parameters can > be passed to the called function by value. I wonder whether passing the parameter > address is possible somehow to allow the function to actually change its value.
On Sat, Jan 3, 2009 at 7:32 PM, <mauede at alice.it> wrote:> I knowf R functions ca be called passing some parameters. > My first question is: how are parameters passed to R functions ? > Browsing through R archives I found an answer confirming taht parameters can > be passed to the called function by value. I wonder whether passing the parameter > address is possible somehow to allow the function to actually change its value.See for instance the R.oo package (on CRAN). That will allow you to emulate pass by reference of R objects (using R environments).> > My second question is about running R scripts with parameters. Something similar to the > s/w device (argc, argv) which is used to run a C program with initial parameters. > Is this possible at all ?See help(commandArgs). /Henrik> > Thank you very much. > Maura > > > > tutti i telefonini TIM! > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Sat, Jan 3, 2009 at 1:32 PM, <mauede at alice.it> wrote:> I knowf R functions ca be called passing some parameters. > My first question is: how are parameters passed to R functions ? > Browsing through R archives I found an answer confirming taht parameters can > be passed to the called function by value. I wonder whether passing the parameter > address is possible somehow to allow the function to actually change its value.Normally this is done by passing back the modified variable as the return value:> a <- 0 > fn <- function(x) x+1 > a <- fn(a) > a[1] 1 Another possibility is to use environments. In the following function fn increments component x of environment e:> e <- new.env() > e$x <- 0 > fn <- function(e) e$x <- e$x + 1 > fn(e) > e$x[1] 1 You can use parent.frame() to refer to the calling environment. ?parent.frame It may be that e above is really best viewed as an object in object oriented programming. In that case the proto package implements such a model in R and R.oo implements a different oo model.> > My second question is about running R scripts with parameters. Something similar to the > s/w device (argc, argv) which is used to run a C program with initial parameters. > Is this possible at all ? >?commandArgs> Thank you very much. > Maura > > > > tutti i telefonini TIM! > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >