An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091105/de0f03ee/attachment-0001.pl>
rm(list<-ls()) error
3 messages · Feng Li, Tony Plate, Uwe Ligges
"<-" 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.
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...
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
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.