[R] Trying to learn how to write a function... can't define a variable??

William Dunlap wdunlap at tibco.com
Fri Sep 7 23:19:52 CEST 2012


>  ADD <- function(x) x <<- x+1 

This would not solve the user's problem.  "x <<- something" assigns something to a variable
called (literally) 'x' in the first ancestral environment of ADD that already contains
a variable called 'x' or in the global environment if 'x' were not found.  E.g.,

if(exists("x")) rm(x)
f1 <- function(x) {
    add <- function(x) x <<- x + 1
    add(x)
    x
}
f1(10) # returns 11
exists("x")  # returns FALSE

add <- function(x) x <<- x + 1
f2 <- function(x) {
    add(x)
    x
}
f2(10) # returns 10
exists("x") # returns TRUE
x # 11

if(exists("x")) rm(x)
f3 <- function(z) {
    add <- function(x) x <<- x + 1
    add(z)
    z
}
f3(10) # returns 10
exists("x") # returns TRUE
x # 11

There is a use for such functionality, but not here.  (Actually, I wish it
would throw an error if 'x' were not found in ancestral environment.)

If one really wants to modify an argument to a function, don't use
<<- or assign in the function but use a replacement function instead.
The following is not a great example, but ...
     `ADD<-`  <- function(x, value) { x <- x + value ; x }
     z <- 10
     ADD(z) <- 7
     z # 17
 This makes it easy to follow the data flow while reading code -- things
on the left side of the <- get created or changed and things on the
right side are not altered.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of David Winsemius
> Sent: Friday, September 07, 2012 1:35 PM
> To: wwreith
> Cc: r-help at r-project.org
> Subject: Re: [R] Trying to learn how to write a function... can't define a variable??
> 
> 
> On Sep 7, 2012, at 11:00 AM, wwreith wrote:
> 
> > I am just starting to experiment with writing a function and have run into
> > what seems like a limitation or more likely a lack of understanding on my
> > part.
> >
> > Very Simple Example:  I want to define a function that does 1+1=2.
> >
> > z<-1
> > ADD<-function(x)
> > {
> > x<-x+1
> > }
> > ADD(z)
> > z
> > output for z is 1 not the expected 2.
> >
> > Now if I were to do print(x+1) instead of x<-x+1 it does return 2, so the
> > function seems ok with x+1, but not ok with x<-. Is there a way to define a
> > variable inside a function or am I violating some rule that I don't know
> > about?
> 
> The rule you are violating is failing to assign the calculated value in the proper
> environment. The x=1 value exists inside the function and _is_ returned, but you didn't do
> anything with it, so it has no name and will get garbage collected. Here's an incrementer
> function that works:
> 
>  ADD <- function(x) assign( deparse(substitute(x)), x+1, envir=parent.frame() )
>  x=1
>  ADD(x)
>  x
> #[1] 2
> 
> You could also have written it thusly:
> 
>  ADD <- function(x) x <<- x+1 )
> 
> (But that operator is frowned upon by those in the know.)
> 
> I'm not sure what sort of reaction would be provoked by:
> 
> ADD <- function(x) {  eval.parent(substitute(x <- x + 1)) }
> 
> The data.table package does in-memory alterations in its objects using a database
> model. It is often much faster than reassignment of dataframes to them self or even
> adding a columns, which does require making a copy (or maybe even two) of the entire
> object.
> 
> --
> David Winsemius, MD
> Alameda, CA, USA
> 
> ______________________________________________
> 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.




More information about the R-help mailing list