Recovering Files After Homebrew Upgrades R: Understanding R’s Workspace and History

-

Recovering Files After Homebrew Upgrades R: Understanding R’s Workspace and History

Summary

A Homebrew command like brew install gcc can trigger an upgrade of R. When this happens, Homebrew removes the old R version from /opt/homebrew/Cellar. Some users worry that unsaved scripts might also be deleted. Fortunately, upgrading R does not remove your personal R scripts or data—those live in your working directory. However, if you exited R without saving, the objects are usually lost. This article explains where R saves your data and how to recover what you can.

Homebrew upgrades do not delete your files

When Homebrew upgrades R (e.g., 4.4.2_24.5.1), it removes the old installation directory but leaves your home directory untouched. Your scripts, data frames and R projects are stored in your working directories (e.g., ~/Documents), not in /opt/homebrew/Cellar. If you started R from a terminal session and did not save your work, there is no hidden backup of the code. Homebrew’s brew cleanup only deletes old binaries.

What can be recovered

  1. Command history: R keeps a record of the commands you enter. The history() function displays recent commands (25 by default), and you can save them to a file with savehistory(). The default history file is .Rhistorystat.berkeley.edu. Loading this file with loadhistory() restores the commands into your session.
  2. Workspace image: When you exit R and choose to save the workspace, R calls save.image() to write all objects to a .RData file in your working directorystat.berkeley.edu. You can reload this file by starting R in that directory and running load(".RData"). If you didn’t save and exit properly, .RData won’t exist, and your objects are lost.
  3. RStudio backups: If you were using RStudio, unsaved source tabs might be recoverable. On Windows, look in %USERPROFILE%\AppData\Local\RStudio-Desktop\sources for files ending in -contentsdatacornering.com. These contain unsaved scripts. Check the history database or the History pane as welldatacornering.com.

Recommendations

  • Save regularly: Run save.image() periodically to create .RData, and keep your scripts in separate .R files using a text editor or RStudio’s save feature.
  • Check the history: Use history(max.show = Inf) to view your entire command history, then copy/paste commands back into a new scriptstat.berkeley.edu.
  • Use version control: For critical scripts, put your .R files in a Git repository. That way, upgrades or crashes won’t wipe your work.

Conclusion

A Homebrew upgrade doesn’t delete your scripts, but R won’t magically recover unsaved work. Familiarize yourself with .Rhistory and .RData files, and save your workspace before exiting. If you were using RStudio, look in the hidden sources directory for unsaved tabs. Otherwise, unsaved code is not recoverable.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent comments