如何阻止RStudio启动时自动加载指定R包?
Let's break down why those packages are loading without you explicitly setting them in your root .rprofile, and walk through how to fix it:
Common Causes & Step-by-Step Fixes
1. RStudio's Workspace Auto-Restore (Most Probable Cause)
RStudio defaults to restoring your previous workspace when you launch it—this includes any packages you had loaded when you last closed the app. That’s almost certainly why RMySQL and DBI keep popping up on startup. Here’s how to disable this:
- Open RStudio, go to
Tools > Global Options > General - Uncheck the box for "Restore .RData into workspace at startup"
- Change the dropdown for "Save workspace to .RData on exit" to "Never"
- Click "Apply" then restart RStudio. This will stop packages from loading just because they were present in your last session.
2. Project-Specific Startup Scripts
For the project where clValid and cluster load automatically, check if there’s a project-level .Rprofile file in the project’s root directory (note: this is a hidden file—on macOS, press Cmd+Shift+. to reveal hidden files).
- If you find this file, open it and look for lines like
library(clValid)orlibrary(cluster). Delete those lines, save the file, and restart the project. - You can also check your project’s
.Rprojfile (open it with a text editor) for anyLoadHookconfigurations that might trigger package loads, though this is less common.
3. System-Level R Configuration
In rare cases, system-wide R settings might be loading packages. To investigate:
- Run
R.home()in R to get your R installation directory - Navigate to the
etcsubfolder and openRprofile.site(you may need admin rights to edit this file) - Look for any
library()calls for the packages you want to block, and remove those lines if present.
4. Verify the Fix
After making these changes, restart RStudio and create a new blank project. Check the startup messages—you shouldn’t see those packages loading automatically anymore. For your existing projects, re-open them and confirm the unwanted package loads are gone.
A quick note: The "Loading required package" message usually means another package is pulling in those dependencies. If you still see the message after disabling workspace restore, double-check that no startup scripts (root or project-level) are loading a package that depends on RMySQL/DBI/clValid.
内容的提问来源于stack exchange,提问作者BillPetti

