Skip to content

rm(list<-ls()) error

3 messages · Feng Li, Tony Plate, Uwe Ligges

#
"<-" and "=" are not universally interchangable.
function (..., list = character(0L), pos = -1, envir = as.environment(pos), 
    inherits = FALSE)


The call
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
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:
Error: object 'b' not found
a= 33 b= 2
[1] 33
a= 1 b= 33
-- Tony Plate
Feng Li wrote:
#
Feng Li wrote:
Yes, and it means that you make an assignment once passed to the first 
argument "..." in rm() and evaluated. Well, it is just never evaluated 
since "..." needs to be a name or a character vector (and is a language 
object in this case), hence an error in rm().

You can do:

rm(list=(list <- ls()))

of course, which does what you are intending, I guess: assigns the ls() 
to list and removes all objects given in list, since list is passed to 
the argument list.

Uwe Ligges