Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear R environment of all objetcs & packages

Tags:

r

I tried this command

remove(list = ls())

I expect to clear all R environment (Objects, packages)

like image 380
Mohamed SELAMA Avatar asked Jul 17 '19 09:07

Mohamed SELAMA


People also ask

How do you clear an entire environment in R?

The console can be cleared using the shortcut key “ctrl + L“.

How do I Rm all objects in R?

Remove Objects from Memory in R Programming – rm() Function rm() function in R Language is used to delete objects from the memory. It can be used with ls() function to delete all objects. remove() function is also similar to rm() function.

How do I restart the R environment?

You can restart R by clicking on Session -> Restart R (top menu).

How do I clear the console in R?

Ctrl+L — Clear the Console. Esc — Interrupt R.


2 Answers

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!

RStudio preferences

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.

like image 123
Konrad Rudolph Avatar answered Oct 23 '22 02:10

Konrad Rudolph


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.

like image 45
Alfonso Avatar answered Oct 23 '22 04:10

Alfonso