I would like to have a simple function to clear my workspace in R, but I seem to be having issues. I have my code below.
clear() = function() rm(list=ls())
When I define this function and call it by simply using clear()
, the code executes but my workspace is not cleared. I tried various formats of defining the function to see if anything funky was going on, but it all gives the same result. Simply using the rm(list=ls())
function works, but not when I embed it in my function. Can anyone point me in the right direction? And what am I not understanding about user-defined R functions?
Thanks!
The problem is that when you call ls()
inside a function it returns the objects in the environment of that function by default. Same with rm()
.
Try this:
clear <- function() {
rm(list=ls(.GlobalEnv), envir=.GlobalEnv)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.