Continuing the discussion from Scripts and dataframes left opened in the previous session are not restored when restarting RStudio (second post):
Hi Everyone,
Previously, I posted about a problem I was having where RStudio was not restoring my open documents upon reopening an RStudio Project. I found a number of posts here on the forums where other people had this issue, but there seemed to be no reproducible reason for the problem. In my case, I discovered both the cause of the problem and a solution for it.
Summary of the Problem
For some of my RStudio projects, upon loading a previously saved RStudio project, RStudio was not restoring to the Source
pane the documents I had open when the project was last saved.
Cause of the Problem
I determined that the cause of the problem was file lock contention between RStudio and the Google Drive Backup Application (GDBA), which was causing some files in the .Rproj.user
subdirectory to become corrupted. Once corrupted, RStudio would not restore the document layout to the Source
pane when the project was reloaded.
How can corrupted projects be fixed?
-
Close the GDBA.
-
Close RStudio.
-
Delete the affected .Rproj.user
subdirectory.
-
Reopen RStudio and load the project.
How can the problem be prevented?
-
Option 1: Stop using the Google Drive Backup application.
-
Option 2: Be sure that the Google Drive Backup application is not running when RStudio is running.
-
Option 3: Do not work on RStudio projects that have been saved to a directory that is monitored by the Google Drive Backup Application.
Detailed Explanation of Problem and Solution
On my Windows 11 computer, I run the Google Drive application, which automatically synchronizes a specified storage location on my local hard drive, as well as all of its subdirectories.
If I open an RStudio project that is located in one of the subdirectories monitored by the Google Drive Backup application (GDBA), the GDBA will notice the file changes made by the RStudio IDE in the corresponding project's .Rproj.user
subdirectory and attempt to start backing up all of the changed files.
I'm guessing that RStudio has a lock on these files, as the GDBA will keep attempting to acquire its own lock on them. Despite being unable to do so, the GDBA will keep trying furiously to get a lock on these files. My assumption is that this file lock contention results in corruption of some files in the .Rproj.user
subdirectory, which is the source of the underlying problem that is the subject of this post.
Unfortunately for users of both RStudio and the GDBA on Windows, neither of these apps actively alert the user when this problem occurs. If a user opens the GDBA and reviews the error log, it will show the files that it is having trouble backing up. However, as soon as the user exits RStudio, the GDBA will successfully complete its backup of these files, and the error logs will not show any problem.
Worse still, upon opening a saved project, the corrupted files in the .Rproj.user
subdirectory will prevent RStudio from restoring the documents in the source pane, but the user will not receive any error notification.
To fix a project where the .Rproj.user
subdirectory has become corrupted, I use the following procedure.
-
If the affected project is located in a directory location monitored by the Google Drive Backup application, be sure to disable the GDBA.
-
Close RStudio.
-
Delete the .Rproj.user
subdirectory within the affected project.
-
Reopen RStudio.
-
Load the affected RStudio project.
Now, the IDE will behave properly and restore the source pane layout configuration when a project is reloaded. If you use this procedure, please note that RStudio will not recovery the layout configuration before the files in the .Rproj.user
subdirectory became corrupted. However, it will work correctly from that point forward.
All of this headache could be avoided if Google would give users of its Google Drive Backup application an option to specify file or folder patters to ignore. As a web search will show, this feature request has been made countless times over the years, and Google has made clear that they aren't going to implement it.
Thus, I have started closing the Google Drive Backup app before I start RStudio. Also, I've stopped creating RStudio projects in folders monitored by the Google Drive Backup application. Instead, I have a Github repo that I use to back up all of my personal RStudio projects. Although I wish I could save my RStudio projects to my Google Drive, this file lock contention conflict has forced me to stop using RStudio to work on projects that are saved to a location monitored by the GDBA.
Truth be told, compared to Google Drive, Github is a better place to store software projects anyway, which includes RStudio projects. Also, because RStudio has direct support git
built in to it, the burden for using this approach is lessened. With this approach, now, all of my small RStudio projects are handled with the same level of care and robustness as my professional software projects.