Creating a function using rm(list = ls())

I'd like to create a function "clearall" which is able to run to code rm(list = ls()) to clear my environment easily.

The problem being that the code run perfectly when executed in the console, but not when I run the function clearall() :

clearall = function(){
rm(list = ls())
}

Does anyone know why and how to fix it please ?

That's because you need to specify which environment you are clearing. By default, rm() clears the current environment, which is the environment inside the function. But you want to clear the global environment. So you need rm(list = ls(), envir = .GlobalEnv)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.