1

I am in the early stages of learning R, and thought this would be a simple enough exercise. would like to write a shortcut function to remove all variables from the workspace. I tried with the following (testing that it was running by using a print statement, initially):

clr<-function(x){ 
  rm(list = ls()) 
  #sprintf('this works')
}

But the above does nothing after I source the code and run the command "clr()" (without quotes, of course).

Do I need to specify the environment (env = globalenv())? I tried this but to no avail.

Thanks in advance!

oguz ismail
  • 1
  • 16
  • 47
  • 69
Nicole Goebel
  • 537
  • 1
  • 9
  • 17
  • Probably a good idea would be to learn not to save a .Rdata workspace in the directory R starts in – mdsumner Feb 12 '14 at 05:19
  • I always change the directory to the one where my R file resides. Is this what you are saying that I should do? – Nicole Goebel Feb 12 '14 at 05:44
  • @mdsumner, I appreciate the other explanation for [Clearing Workspace in R](http://stackoverflow.com/questions/20389821/clearing-workspace-in-r), however I like how the solution below avoids removing the object function itself. – Nicole Goebel Feb 12 '14 at 05:49
  • If you start R in a folder containing a *hidden* .Rdata file R's default is to load it. rm(list = ls()) is a nasty virus that should be replaced by the full explanation. It's annoying, slows down your startup and could hide other nasty surprises. – mdsumner Feb 12 '14 at 12:36

2 Answers2

4

There are two issues with your function related to environments: both ls() and rm() work in the current environment, which will be the executing frame of the function clr().

In other words, ls() will give you the objects available in the frame of clr(), which is nothing in the case of your function (debug it and see what ls() returns once you are in the execution frame of clr()). Hence you were asking rm() to remove nothing.

Once you fix that, you still need to tell rm() which environment to remove objects from. Here you made another error; the first argument to rm() is ..., which means that you need to fully name arguments to rm() that come after the ... argument. The argument you want is envir not env.

Here is a function which does what you wanted, plus it won't delete itself (!).

clr <- function() {
  ENV <- globalenv()
  ll <- ls(envir = ENV)
  ll <- ll[ll != "clr"]
  rm(list = ll, envir = ENV)
}

In use we have

> ls()
[1] "clr"  "obj1" "obj2"
> clr()
> ls()
[1] "clr"

Note that as written this won't delete hidden objects (those with a . as the first character of their name). For that you need to use ls(all.names = TRUE).

A final remark, but you don't need the argument x in the definition of clr(); functions with no arguments are fine.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • I had used the `.` approach in naming the function in my answer here: http://stackoverflow.com/a/20389913/1270695 – A5C1D2H2I1M1N2O1R2T1 Feb 12 '14 at 04:55
  • thanks so much, that makes perfect sense. In R, is there a startup file that is run when R is opened, so that I do not have to source the file that this function lies in, every time I use R? – Nicole Goebel Feb 12 '14 at 05:42
  • @AnandaMahto I considered naming the function with a `.` to hide it but then realised it would still get cleaned if you wanted to remove hidden objects too. I don't think there is a neat solution to this other than to package it and load as with any other R package, or `attach()` the function to the search path, then it doesn't matter if the one in the workspace gets deleted. – Gavin Simpson Feb 12 '14 at 15:50
  • 1
    @NicoleGoebel Yes, there are several depending on your OS, how it was installed and what admin rights you have. See `?Startup`. The other option, which is preferable, would be to place this and any other useful functions you might write into a personal R package, which you then load using the methods/files mentioned in `?Startup`. – Gavin Simpson Feb 12 '14 at 15:52
  • Thanks @GavinSimpson, I will have a look at the startup help and go from there. I have already put this function into a file that I hope to add other "shortcuts" to. Starting to really like R, the more I learn! – Nicole Goebel Feb 12 '14 at 17:53
  • @NicoleGoebel Better to have a package than a R script of functions. You don't need to document all your functions to write a package. It is also good practice learning how to package, NAMESPACEs etc. – Gavin Simpson Feb 12 '14 at 17:58
  • Thanks Gavin. I will see if I can figure out how to make a package. If you happen to have a link or useful document to point to, that would be great. Otherwise I am sure there is more than enough information out there to sift through! – Nicole Goebel Feb 12 '14 at 19:55
1

My take is that this is the wrong question. The rm(list = ls(...))construct is simply a nasty virus propagating by code copied between users.

What is the need for this construct?

  • tidy workspace
  • reduce memory use
  • simplify the web of dependencies (rm won't help this)

Why wouldn't the designers of R provide a clean workspace to begin with? They do, it just gets misused and the default behaviour of Windows hides the details in unhelpful ways.

Why is the workspace not clean when R is started?

When R is started, it always works with a "current directory", that's like being "in a folder", if you ask for the "file1.iaf" you don't need to explicitly point to "C:/temp/myfolderofiafs/file1.iaf".

In unix that's just the place you're at when you type

R

and you can see it directly when you type

pwd

In Windows, it's the same at the command line (replace pwd with dir and look for "Directory of ..."), but when you "(double)click on the R shortcut" the system does the same for you, but where it starts is controlled by the "Properties" of the shorctut. You can see this by right-clicking on the shortcut, click "Properties" and see what the "Start in" field says. (It might be a system wildcard like %HOMEPATH% or whatever, but that probably stands for "C:/Users/username/Documents".)

Right-click on Properties

enter image description here

  1. R defaults to starting in one of your folders because it's the only place it can really know that is has permissions to do stuff.

  2. I'm using Windows 8, this is all slightly different on Windows 3.1, 95, 98, NT, CE, 2000, XP, Vista, and 7, but covering all that can be left to google.

Have a look in that "Start in" folder, the chances are you won't see a file with the name ".Rdata" but that is because it is hidden. Start R, and run the following code before doing anything else:

list.files(all = TRUE)

Is there a ".Rdata" file? (You can use "dir /a" to see it at the command line, or "ls -a" in unix for the same.)

When you start R that file gets loaded by default, as if you had done the following:

load(".Rdata")

That's why the workspace "needs cleaning up", but in my opinion you should delete that ".Rdata" file and rid your workflow of clunky hacks.

You can fix this from R, do this:

unlink(".Rdata")

and when you quit R, select "no" so that this hidden file does not get re-created again. If you accidentally create it, hunt it down and kill it again.

If you really want to save your entire workspace, use:

save.image("myentireworkspaceorperhapssomesensiblefilenameforit.Rdata")

or just a few objects from it:

save(keepme, keepme2, andme, file = "justsomeofmyclingyobjects.Rdata")

You can see the hidden ".Rdata" file in Windows itself by setting the View Properties for the folder (this is Windows 8 where I get to it via "View/Options 'Change folder and search options'"):

Really, just show me what's actually here

Choose "Show hidden files, folders and drives". I also strongly recommend that you unclick "Hide extensions for known file types" because the extension really is part of the file-name and hiding it sometimes just confuses everything. (Windows by default would make your file's real name "file.R.txt" if you tried to rename it from "file.txt" to "file.R" without this set).

There is more to this, in the "Target" field of the Properties of the shortcut you can add arguments such as "--no-restore" or "--no-restore-data".

Not ideal, but it's an option

This would also "fix" the ultimate problem above, but not in the right way IMO.

These arguments are available at the command line with (on Windows):

Rgui --help

but you'll need to use the full path or have R's bin/* folders in your path for that to work. See ?Startup for a fuller discussion,

mdsumner
  • 29,099
  • 6
  • 83
  • 91
  • 1
    This seems a little over the top, with hunting and killing and all :) but I think that's just because I don't encounter the problem being described. I use `rm(list=ls())` because I have a session running already and want to make sure my script works on its own, without the stuff I've been interactively defining (and not because I double-clicked the R icon and bumped into `.RData`). I almost run R by double-clicking "name.RData" or using the session nppToR spawns. – Frank Feb 13 '14 at 21:44
  • 1
    Ok, I'll save it for when I have the target right. – mdsumner Feb 13 '14 at 23:39
  • Thanks mdsumner. I will have a look at the things you mention in order to properly understand what R is doing when it starts up. I like to "clear my workspace" for the same reasons as @Frank. But perhaps I will run into issues you refer to in the future. Thanks very much for all the information! – Nicole Goebel Feb 14 '14 at 00:59