0

I just got started learning R with the "datacamp" site and I ran into a syntax misunderstanding at the beginning.

It says that rm(list = ls()) is a very useful command to clear everything from your workspace but I don't understand what list = is for.

  • a. They haven't yet taught me the meaning of = in R and I didn't find an explanation at the documentation. = is like <-? What's the difference?

  • b. If the input of rm() can be a list of variables names, and the output of ls() is a list of var names, why can't I just use rm(ls())?

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
Miki
  • 51
  • Do `help(rm)` to see what parameters `rm` takes. `list` is the name of one of the parameters. You can supply parameters to functions positionally and/or with explicit names. The reason for the explicit name here that the function parameter list starts with `...`. If you don't specify one of the named parameters by name, `rm` has no idea of your intent since you can remove any number of individual objects in the environment with `rm` in the same call. This is the only time (IMO) that you should use `=` is when performing function parameter assignments. – hrbrmstr Sep 18 '15 at 17:37
  • Dupe #1: http://stackoverflow.com/questions/1741820/assignment-operators-in-r-and – Rich Scriven Sep 18 '15 at 17:41
  • 2
    Short answer, `ls()` has quotes (is a character vector) and the `...` argument in `help(rm)` says it only accepts unquoted names – Rich Scriven Sep 18 '15 at 17:46
  • 3
    This is not a bad question. Ignore the downvotes. – Pierre L Sep 18 '15 at 19:11

1 Answers1

4

Passing arguments by position vs name

The = symbol plays a special role in naming arguments to a function call.

Consider two essentially identical functions:

f <- function(..., y=3) (2+sum(...))^y
g <- function(y=3, ...) (2+sum(...))^y

If y= is not named, the results are generally different:

f(y=5) # 32
g(y=5) # 32

f(5)   # 343
g(5)   # 32

rm is like f -- type ?rm to see -- so if you want to call rm(list = ls()), write it out in full.

Representing object names

In most of R, if you write f(g()), evaluation flows naturally:

  • g() is evaluated to 8 and substituted into f(g()) for f(8)
  • f(8) is evaluated to 1000

rm breaks this pattern in its unnamed ... arguments, which basically just exist for interactive use. Only manually typed variable names are allowed.† As a result, rm(ls()) won't run.

Hadley Wickham provides another nice example:

ggplot2 <- "plyr"
library(ggplot2) # loads ggplot2, not plyr!

† Okay, you can use the ... without manually typed names, like

do.call(library, as.list(ggplot2)) # loads plyr!

but don't mess with that unless you know what you're doing.

Frank
  • 66,179
  • 8
  • 96
  • 180
  • But also note that if you have `X` and `Y` in the workspace, with `rm()` you **can** do `rm("X", "Y")` but you **cannot** do `rm(ls())`. This is what I am having trouble grasping. – Rich Scriven Sep 18 '15 at 18:47
  • @RichardScriven Yeah, even if there's just `x`, like `rm(list=ls()); x=1`, you can't do `rm(ls())`... I'm trying to think of some other examples with such abnormal evaluation rules. Oh, here's one: `mylib = "data.table"; library(mylib) # error`. I'm guessing it's most prevalent in old base functions and the hadleyverse. – Frank Sep 18 '15 at 18:49
  • 1
    So, something happens internally which makes `rm(ls())` impossible. Note that `.Internal(remove(ls(), .GlobalEnv, FALSE))` does work, and `rm()` calls this at the end (for the basic case). – Rich Scriven Sep 18 '15 at 19:05