在 macOS Catalina 上,在 RStudio 中安装包失败

Installing a package inside RStudio fails on macOS Catalina

macOS Catalatina, 上尝试在 RStudio 中安装软件包时失败并出现 Permission Denied 错误:

> install.packages("mlbench")
trying URL 'https://cran.rstudio.com/src/contrib/mlbench_2.1-1.tar.gz'
Content type 'application/x-gzip' length 920768 bytes (899 KB)
==================================================
downloaded 899 KB

* installing *source* package ‘mlbench’ ...
** package ‘mlbench’ successfully unpacked and MD5 sums checked
mv: rename /usr/local/Cellar/r/3.6.1_1/lib/R/library/mlbench to /usr/local/Cellar/r/3.6.1_1/lib/R/library/00LOCK-mlbench/mlbench: Permission denied
ERROR: cannot remove earlier installation, is it in use?
* removing ‘/usr/local/Cellar/r/3.6.1_1/lib/R/library/mlbench’
Warning in install.packages :
  installation of package ‘mlbench’ had non-zero exit status

The downloaded source packages are in
    ‘/private/var/folders/bc/wsz930gj3gn5zgq4hbm6jkfr0000gn/T/RtmpdvQLox/downloaded_packages’
Updating HTML index of packages in '.Library'
Warning in install.packages :
  cannot create file '/usr/local/Cellar/r/3.6.1_1/lib/R/doc/html/packages.html', reason 'Permission denied'
Warning in install.packages :
  cannot update HTML package index

如 RStudio 支持所述here

Are you able to install packages in R?

If you're having issues, we recommend trying to install packages in R (outside of RStudio) and see if you're able to do that. If not, please check the possible solutions below.

因此,需要:

  • 切换到Terminal.app,
  • 在您的终端内,您的 R 控制台 必须sudo 权限启动。否则,它将再次失败并出现权限被拒绝的错误。因此,在您的终端中,键入 sudo R
  • 然后安装任何必要的包:例如install.packages("mlbench").