5

I am trying to find a way to clear the workspace in R using lists.

According to the documentation, I could simply create a vector with all my workspace objects: WS=c(ls()). But nothing happens when I try element wise deletion with rm(c(ls()) or rm(WS).

I know I can use the command rm(list=ls()). I am just trying to figure how R works. Where did I err in my thinking in applying the rm() function on a vector with the list of the objects?


Specifically, I'm trying to create a function similar to the clc function in MATLAB, but I am having trouble getting it to work. Here's the function that I've written:

clc <- function() { rm(list = ls()) }
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
jessica
  • 1,325
  • 2
  • 21
  • 35
  • Also discussed here: http://stackoverflow.com/questions/3536036/rmlist-ls-doesnt-completely-clear-the-workspace – ToNoY Dec 05 '13 at 02:05
  • I am trying to find a simple way of clearing the WS in Rstudio. I tried creating a function, similar to the `clc` function in matlab to clear the workspace but I am having trouble getting it to work. It seems, I can't create a function in R without an input. clc=function(){ rm(list=ls()) } – jessica Dec 05 '13 at 02:11

1 Answers1

9

From ?rm, "Details" section:

Earlier versions of R incorrectly claimed that supplying a character vector in ... removed the objects named in the character vector, but it removed the character vector. Use the list argument to specify objects via a character vector.

Your attempt should have been:

rm(list = WS)

HOWEVER, this will still leave you with an object (a character vector) named "WS" in your workspace since that was created after you called WS <- c(ls()). To actually get rid of the "WS" object, you would have had to use rm(WS, list = WS). :-)


How does it work? If you look at the code for rm, the first few lines of the function captures any individual objects that have been specified, whether quoted or unquoted. Towards the end of the function, you will find the line list <- .Primitive("c")(list, names) which basically creates a character vector of all of the objects individually named and any objects in the character vector supplied to the "list" argument.


Update

Based on your comment, it sounds like you're trying to write a function like:

.clc <- function() {
 rm(list = ls(.GlobalEnv), envir = .GlobalEnv)
}

I think it's a little bit of a dangerous function, but let's test it out:

ls()
# character(0)
for (i in 1:5) assign(letters[i], i)
ls()
# [1] "a" "b" "c" "d" "e" "i"
.clc() 

ls()
# character(0)

Note: FYI, I've named the function .clc (with a dot) so that it doesn't get removed when the function is run. If you wanted to write a version of the function without the ., you would probably do better to put the function in a package and load that at startup to have the function available.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • to really grab everything, you will need to set `all=TRUE` inside `ls()` otherwise you will leave behind any object that starts with a `.dot` – Ricardo Saporta Dec 05 '13 at 04:26
  • You can remove the function without issue. The current instantion of it will execute without problem but it will be removed from memory – Ricardo Saporta Dec 05 '13 at 15:04
  • 2
    I'm new to R, but one of the first things I've done is create a custom package for all of the things that I find handy (cls/clr being the first two). It doesn't appear that there is a need to ignore "." prefixed functions/data as the library will still be loaded after the workspace is cleared. – Steven Evers Oct 05 '14 at 20:04
  • @SnOrfus, yes, that's true for a package but not for a function created in a session. – A5C1D2H2I1M1N2O1R2T1 Oct 06 '14 at 01:02