Mac系统下如何删除R中全部已安装包?解决yaml缺失报错
yaml Package Error & Deleting All R Packages on Mac Hey there, let's work through this issue together. First, I want to point out that you might not need to delete all your packages to fix that yaml error—let's start with the simpler solution, then cover the full package removal if you still need it.
Fixing the "no package called ‘yaml’" Error
The error pops up because RStudio relies on the yaml package for R Markdown functionality, and it's either missing or corrupted. Here's how to install it directly (bypassing any RStudio-specific glitches):
- Open Terminal on your Mac (find it in Applications > Utilities).
- Type
Rand hit Enter to launch the R command line. - Run this command to install
yamlalong with all its required dependencies:install.packages("yaml", dependencies = TRUE) - Once installation finishes, quit R with
q()and restart RStudio—your error should disappear.
Deleting All User-Installed R Packages (If You Still Need To)
If you really want to wipe all packages you've installed (leaving R's critical built-in base packages untouched), you have two reliable methods:
Method 1: Delete Packages via R Command Line
This is the safer option because it automatically avoids R's essential base packages:
- Launch R via Terminal or RStudio.
- Run this code block to identify and remove all non-base packages:
# Get list of all installed packages all_installed <- installed.packages()[, "Package"] # Get list of R's built-in base packages (do NOT delete these!) base_packages <- rownames(installed.packages(priority = "base")) # Filter out base packages to get the ones you can delete to_remove <- setdiff(all_installed, base_packages) # Batch delete the target packages remove.packages(to_remove)
Method 2: Delete Packages Directly via Finder
This hands-on approach works great if R isn't launching properly:
- Close RStudio and all R processes (check Activity Monitor to ensure no R instances are running in the background).
- Open Finder, press
Command + Shift + Gto open the "Go to Folder" window. - Paste the correct path for your R installation:
- For default system R:
/Library/Frameworks/R.framework/Versions/[Your R Version]/Resources/library(replace[Your R Version]with your installed version, like4.3.1—check the Versions folder to confirm) - For Homebrew-installed R:
/usr/local/lib/R/site-library
- For default system R:
- In the library folder, delete every folder except these built-in base packages:
base,datasets,utils,grDevices,graphics,stats,methods - Restart RStudio, and all your user-installed packages will be gone.
Pro Tip Before Deleting
Before wiping everything, save a list of your current packages so you can reinstall them later easily:
write.csv(installed.packages()[, "Package"], "my_r_packages.csv", row.names = FALSE)
内容的提问来源于stack exchange,提问作者company-slave

