[R] rm(list<-ls()) error

Tony Plate tplate at acm.org
Thu Nov 5 21:42:42 CET 2009


"<-" and "=" are not universally interchangable.

> args(rm)
function (..., list = character(0L), pos = -1, envir = as.environment(pos), 
    inherits = FALSE)


The call
> rm(list <- ls())
assigns the result of ls() to the variable 'list' and passes that value as an anonymous argument to rm() (Probably more than you want to know: or it would if rm() didn't have non-standard evaluation rules -- as it happens, list <- ls() is recognized as an invalid argument before it is evaluated.)

The call
> rm(list=ls())
calls rm() with the 'list' argument having the value of ls()

Here's an example that doesn't confuse things by having non-standard evaluation rules:

> f <- function(a=1, b=2) cat("a=", a, "b=", b, "\n")
> b
Error: object 'b' not found
> f(b <- 33)
a= 33 b= 2 
> b
[1] 33
> f(b=33)
a= 1 b= 33 
> 

-- Tony Plate



Feng Li wrote:
> Dear R,
> 
> Why rm(list<-ls()) gives an error but rm(list=ls()) not? I remember the
> operator ‘<-’ can be used anywhere...
> 
> 
> Thanks!
> 
> 
> Feng
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> 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