Solve an error message "Error: Unable to establish connection with R session" in RStudio

Solve an error message "Error: Unable to establish connection with R session" in RStudio

When I was working with relatively big data (state-wide 1 meter digital elevation model for entire Tennessee and Virginia), I encountered a problem with the error message saying, "Error: Unable to establish a connection with R session" every time I attempted to reopen RStudio.

After searching for the RStudio Support, Stackoverflow, and this blog post, I reach the conclusion that the problem is the huge .RData in my home directory. I saved 11 GB of raster data to .RData in my default directory (/home/myname/ in my Ubuntu machine) to save my computation time.

RStudio (perhaps R itself) seems to automatically loads .RData at the default directory. So if you save the huge data in .RData at the default directory, opening RStudio results in the loading of the huge data to your memory space. So it should be avoided to save a huge amount of data in .RData file at your default directory.

Once you run into the error message above, the easiest solution is to find out .RData file in your directory and run

rm .RData

Then, the problem goes away.

Although the size of the data that can be saved in .RData depends on the size of the memory of your computer but as a rule of sum, more than 100 MB of the data should be saved in an individual .RData file with a proper name to avoid this problem.