1

I'd like to clear the environment during a script run.

I am using of the (slightly changed) function which i've seen here

clearEnv <- function(env = parent.frame())
{
  rm(list = setdiff( ls(all.names=TRUE, env = env), lsf.str(all.names=TRUE, env = env)),envir = env)
}

But this only works outside of a script. I want to call this function in-script of my personal R package before anything else is saved as variable.

Is there a way of doing this? Maybe nesting the function somewhere?

Thank you!

EDIT: Thought it was clear enough - obviously not, so here is a minimal example:

script.R has two functions

clearEnv <- function(env = parent.frame())
{
  rm(list = setdiff( ls(all.names=TRUE, env = env), lsf.str(all.names=TRUE, env = env)),envir = env)
}

myScript <- function(){
 clearEnv()
 /*do work*/
}

-- This does not work. I see that it is not clearing the environment at all, because when I save a variable, say thisShouldVanish <- 0, before running myScript(), it is still there afterwards.


If I run

clearEnv()
myScript()

it is working as expected, but I want to run myScript() only instead of separating the two function calls...

I mentioned, that this functions are part of a package, so neither these function nor anything else has to be in the environment -> it should be okay to delete mid-run.

I hope I was able to explain it better.

Community
  • 1
  • 1
MineSweeper
  • 584
  • 1
  • 6
  • 18
  • What do you mean "outside of a script?" What exactly does your script look like? How are you calling it? And how do you know it's not working? A [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would be very helpful – MrFlick Jun 08 '15 at 16:51

0 Answers0