软件包返回“创建锁定目录失败”

我在下载 Rcpp 软件包时遇到这个错误:

> install.packages("Rcpp", dependencies=TRUE)
Installing package(s) into ‘/home/me/src/Rlibs’ (as ‘lib’ is unspecified)
trying URL 'http://cran.us.r-project.org/src/contrib/Rcpp_0.10.2.tar.gz'
Content type 'application/x-gzip' length 2380089 bytes (2.3 Mb)
...
Warning in dir.create(lockdir, recursive = TRUE) :
cannot create dir '/home', reason 'Permission denied'
ERROR: failed to create lock directory ‘/home/me/src/Rlibs/00LOCK-Rcpp’
...

由于我的机器是在一个计算机集群上运行的,所以我在不同的节点上尝试了它,并且小心地删除了/tmp 中下载的临时文件。奇怪的是,我有权在/home/me/src/Rlibs/中写。所以我的问题是:

  1. 为什么 R 想要在/home 中拥有写作权,而它只需要在/home/me/中拥有写作权?
  2. 我如何修正错误?

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-redhat-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=C                 LC_NAME=C
[9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
loaded via a namespace (and not attached):
[1] tools_2.15.2
102951 次浏览

On NFS file systems it is sometimes not obvious what things you have to close.

The best way to avoid this is to use the --no-lock argument on the command line, i.e.:

R CMD INSTALL --no-lock <pkg>

From within R, you can do this from within your command using:

install.packages("Rcpp", dependencies = TRUE, INSTALL_opts = '--no-lock')

This happens when your last package installation has interrupted abnormally. to fix this you should remove the locked file. For example Execute this command in R console:

unlink("/home/me/src/Rlibs/00LOCK-Rcpp", recursive = TRUE)

Hope this helps!

I encountered a similar problem running windows 7:

Error in install.packages : ERROR: failed to lock directory ‘D:\Program Files\R\R-3.6.2\library’ for modifying.

I solved this problem with below command in the R-console:

unlink("D:\\Program Files\\R\\R-3.6.2\\library/00LOCK", recursive = TRUE)

Hope this help windows users...

This can happen when you are upgrading to a major version of the R as well. Some major upgrades require you to rebuild your packages, e.g., R 4.0. In my case, I had installed R using Homebrew, brew install R and maintained it for a long time, but when I upgraded to 4, I had to build the packages again and ran into this problem.

To resolve it, you need to make sure that Homebrew removes leftovers of your older R installation. In default setting, you can find them here, /usr/local/lib/R. I had instances of 3.5, and 3.6 besides the rest of R's internal. You can remove everything there, and install R again, and everything should work fine. Or just remove the older version of R, and empty the 4.0. I recommend doing a clean install though.

So, if you are maintaining your R using Homebrew and ran into this problem, here is how you fix it:

brew uninstall R
rm -r /usr/local/lib/R
brew install R

The easiest way to avoid this issue is before installing any package run the line below

options("install.lock"=FALSE)

Then try the install.packages("name_of_package") to install the package. The 00LOCK error would not come.