This is because rm()
uses non-standard evaluation by default. It tries to remove what you literally type for the parameters that are not list=
(or pos=
or env=
or inherits=
).
So you can do
a <- 10
rm(a)
Note that a
is just interpreted as a symbol. It's not evaluated to return a value. When you call rm(list<-ls())
you are expecting that expression to be evaluated, but it is not. It's trying to find a variable named "list<-ls()"
but no such variable exists because no variable should have such a name. Additionally it has to be a "valid" variable name. From the ?make.names
help page
A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number
This means that it will not parse unusual variable names like those you can make by escaping symbols with back ticks or single quotes. Technically you can also do
a <- 10
rm("a")
because the non-standard evaluation checks if the parameter is a literal character value. But it will still not evaluate any expressions even if they will ultimately return a character value. For example
a <- 10
b <- "a"
rm(b)
What happens above is that b
is removed, not a
.
If you want to pass in a function that returns names of variables as stirngs (as ls()
does), you need to use the named list=
parameter. The <-
operator does not work as a substitute for =
for named parameters.