clearing the environment in r

How To Clear The Environment in R: Keep RStudio from Running Slow

Knowing how to clear the environment in R is one of the easiest ways to overcome an RStudio installation that is running entirely too slow. To that end, RStudio can slow to a crawl due to a multitude of reasons ranging from a large amount of information being stored in memory, settings being set in an unhelpful way, or the data you are working with being too large for smooth operation.

Fortunately, there are quick and easy ways to go about making corrections to the first two issues. On the other hand, working with large data is sometimes a problem that requires other solutions (like installing RStudio onto an Amazon AWS instance).

As is the case when looking to solve most problems in RStudio, it is a good idea to start with the “low hanging fruit.” In other words, let’s start by taking a look at functions and settings that you can edit in just seconds to try to speed up your installation of RStudio. In both cases, each is a way to clear the environment in R.

Clear The Environment in R: Using Garbage Collection

A fast and easy way to clear the environment in R is to make use of the gc() function.

The gc() function will allow you to automatically clear memory on your computer only after an object is no longer being used within the RStudio environment. it is important to note that this is not something you need to keep track of yourself. The gc() function internally tracks which names point to which objects in your environment. And, once you run the function, it will determine which objects have no names pointing to them, and then delete that object.

The end result is the release of unused memory into the RStudio environment. Running a garbage collection to clear the environment in R and free up memory is super easy:

gc()

> gc()
           used  (Mb) gc trigger  (Mb) max used  (Mb)
Ncells  4064267 217.1    6912572 369.2  6912572 369.2
Vcells 56068236 427.8   89642141 684.0 89635892 683.9

By simply running gc(), RStudio automatically does the work and reports back with the amount of memory cleared from the environment which is now available for you to use.

As well, depending on which version of RStudio you are running, there is now the ability to free unused R memory directly from the environment tab.

RStudio users that have updated to the latest version can simply click on ‘Free Unused R Memory’ within the environment tab and the base R gc() function will run automatically, still providing detailed numbers once the process is complete.

Clear the Environment in R: Disable The Use of Real Time Spell-Checking

I personally have not noticed much of a difference in RStudio speed by disabling the use of real-time spell checking in the R environment, but many other users have reported an increase in speed after doing so.

While disabling it may not be the answer for everybody, it is certainly worth a try since it is such a quick and easy thing to do.

clear the environment in r

To be honest, I do not really pay much attention to the spell-checking process within RStudio. Oftentimes, I provide my functions and dataframes names that will trigger any spell-checking software.

Because of that, I regularly have this disabled. Given that reasoning, as well as the fact that it could speed up your RStudio installation, it is worth disabling on your end as well.

Other Easy Ways To Speed Up Your RStudio Environment

If you have run the gc() function as well as disabled spell-checking within RStudio, and yet your install is still lagging, there are other small things you can try in an attempt to clear the environment in R.

  1. Set your Zoom to 100%. You can set this by going to [Global Options] –> [Appearance] –> [Zoom]. I assume that any zoom setting over 100% has the potential to cause your RStudio environment to lag. Personally, I run my zoom at 90% and do not recognize any issues with lag. That said: if your settings were, for some reason, set above a zoom of 100%, bring it back down and see if that helps.
  2. Disable Diagnostics in R. You can do this by going to [Global Options] –> [Code] –> [Diagnostics]. Once there, uncheck the option that read ‘Show Diagnostics for R.’

Clearing the Environment in R When Working With Large Data

If you are working with large data (as in gigabytes of it), it is more than likely that your environment in R is going to bog down. For example, I regularly work with Zillow ZTRAX data which results in dataframes in my R environment that are sometimes as large as three to four gigabytes.

When this is the case, many – if not all – of the above approaches are useless. I have 32GBs of RAM in my custom-built computer and it still struggles to work with ZTRAX data.

If you find yourself in this scenario, you typically have one of two options:

  1. Take your data wrangling and manipulation work to the Amazon Cloud (my tutorial on this is coming soon!).
  2. Use a function like fread() from the data.table package to help you grab just the variables you need from the data.

In the case of the fread() package, you can see how I make use of it in my post where I provide a tutorial on importing, cleaning, and manipulating Zillow TRAX data.

Concluding Thoughts

While there are no “100% for sure” answers to clearing your environment in R and speeding up your install, the above options are very good places to start. I am willing to bet that most issues are solved by employing one or more of them.

That said, if you still have issues after trying the above, feel free to reach out and let me know. We may have to do some digging under the hood of your RStudio install to get to the root of the problem.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit