Preventing RStudio from Freezing
Sometimes, you may encounter a situation in which you open the IDE RStudio with scripts still opened, and it becomes unresponsive immediately. Read on to find out how to solve this issue!
First, some clarification is needed on the conditions for the event described: You were working on a RMarkdown script, with output being printed in-line, i.e. with plots, test, error- and warning messages being displayed between the code chunks. At one or multiple points in your script, your code generated a vast amount of error or warning messages, e.g. as the result of a loop containing a try function, which allows to "skip" (expected) errors for some iterations, but will still print the error message in every iteration that an error occurs. You then close RStudio without closing the RMarkdown script beforehand (or RStudio crashed due to some issue and closed automatically). Now, when you open RStudio again, the RMarkdown script will also automatically be opened, and all in-line output will be loaded and displayed (before you run any of the code yourself). Loading all the error messages requires an excessive amount of time (especially when the loop was over a lot of iterations), leading RStudio to become unresponsive.
Of course, the intuitive solution would be to quickly close the script right after re-opening RStudio, before any in-line output is loaded. However, one is never quick enough to do this - RStudio becomes unresponsive too fast. Now, the in-line output does not just appear out of thin air! Since the code in the script is run neither by the user nor by the machine itself, the in-line output must be stored somewhere on the hard drive of the computer, and must be sourced by an internal script specifying the RMarkdown setup. Locating and modifying this script is not a good idea, since it will probably be written in a way not understandable to most users. Locating and deleting the saved in-line output on the hard drive appears to be a more promising approach.
On Linux, these files are located in a hidden directory /home/user/.rstudio-desktop/notebooks. The folder .rstudio will not be immediately visible (this is true for all folders bearinga leading dot). However, the file browser (e.g. Nautilus in Ubuntu) allows viewing it by making a little change in the file browser's configuration. In Nautilus, you can simply check the box next to Show hidden files after clicking on the menu icon in the upper right of the screen (denoted by three horizontal lines). After navigating into the notebooks directory, you will see several subdirectories bearing the names of various RMarkdown scripts you have been working on in RStudio in the past, extended by some leading cryptic letters and digits. If you want to investigate a bit, you can open these notebook folders, the contents of which depend on the design of your RMarkdown script. You will often find several nested sub-directories, the terminal of which contains some .csv files containing lines of code (from the RMarkdown script) and / or output like warnings. So the notebook directories are actually the containers that cause all the trouble in opening RStudio!
The simple solution now is to delete the respective notebook directory causing the trouble. Personally, I have not found any side effects of doing so, although admittedly I have not yet made much use of this option up to now.
On Windows computers, these directories are located in a different location, owing to the differences between the Linux and Windows operating systems. Here, the notebooks may be found in the directory C:/Users/user/AppData/Local/RStudio-Desktop/notebooks. The AppData directory is not directly accessible through the graphical user interface of the Windows file browser, so the path given aboce must be entered into the search field of the file browser. Then, as in the Linux OS, the notebook folders can easily be deleted.
This should hopefully enable you to work in RStudio with a little less stress. Happy coding!