I tried this command
remove(list = ls())
I expect to clear all R environment (Objects, packages)
I tried this command
remove(list = ls())
I expect to clear all R environment (Objects, packages)
The simplest and, more importantly, the only reliable way of doing this is to restart R. That takes care of everything.
Just make sure you’re not accidentally saving the current R image when quitting R.
In RStudio, you need to set the option “Save workspace to .RData file on exit” to “Never”, and disable restoring upon restart — this is strongly recommended!
After that, make sure that any previously existing .RData
files in your project’s folder are deleted (heads up: .RData
is an invisible file so you won’t normally see it in a file browser; you can delete it via the command line).
To restart R from within RStudio, you can use “Session” › “Restart R” or Cmd+Shift+F10.
The answer was already out there :-) https://stackoverflow.com/a/7506112/7902133
According to this answer, the following code should work
lapply(paste("package:", names(sessionInfo()$otherPkgs), sep=""),
detach,
character.only = TRUE,
unload = TRUE)
You may also want to check the first answer for a full description.
The freshr
package has consolidated the previous answers into one simple function. Install it via
install.packages("freshr")
and then run
freshr::freshr()
in your console and it will unload all packages and variables for you.