今天升級rlang的時(shí)候報(bào)了錯(cuò):
ERROR: failed to lock directory 'C:/Users/MSI-NB/Documents/R/win-library/4.0' for modifying
Try removing 'C:/Users/MSI-NB/Documents/R/win-library/4.0/00LOCK-rlang'
Warning in install.packages :
installation of package ‘rlang’ had non-zero exit status
這時(shí)如果查看對應(yīng)安裝包的文件夾(例子中是win-library/4.0)歼培,會(huì)發(fā)現(xiàn)多了一個(gè)叫做“00LOCK-rlang”(或者直接叫“00LOCK”)的文件夾。
先放解決方案:
方案1:install.packages() 加上INSTALL_opts = '--no-lock':
install.packages("your_package", INSTALL_opts = '--no-lock')
方案1會(huì)安裝升級成功于微,但是00LOCK-rlang文件夾還在——說明下次更新此包時(shí)仍可能出同樣的error。
方案2:use unlink() to delete 00LOCK-rlang
unlink("C:/path_to_your_pkgs/00LOCK-rlang", recursive = TRUE)
刪除00LOCK-rlang文件夾加匈,后續(xù)照常安裝即可。如果unlink失敗可嘗試重啟R淋叶。
ERROR原因:
install.package()的說明文件里是這么解釋的:
Locking
There are various options for locking: these differ between source and binary installs.
By default for a source install, the library directory is ‘locked’ by creating a directory00LOCK
within it. This has two purposes: it prevents any other process installing into that library concurrently, and is used to store any previous version of the package to restore on error. A finer-grained locking is provided by the option --pkglock which creates a separate lock for each package: this allows enough freedom for parallel installation. Per-package locking is the default when installing a single package, and for multiple packages whenNcpus > 1L
. Finally locking (and restoration on error) can be suppressed by --no-lock.
For a macOS binary install, no locking is done by default. Setting argumentlock
toTRUE
(it defaults to the value of[getOption](https://link.zhihu.com/?target=https%3A//www.rdocumentation.org/link/getOption%3Fpackage%3Dutils%26version%3D3.6.2)("install.lock", FALSE)
) will use per-directory locking as described for source installs. For Windows binary install, per-directory locking is used by default (lock
defaults to the value of[getOption](https://link.zhihu.com/?target=https%3A//www.rdocumentation.org/link/getOption%3Fpackage%3Dutils%26version%3D3.6.2)("install.lock", TRUE)
). If the value is"pkglock"
per-package locking will be used.
If package locking is used on Windows withlibs_only = TRUE
and the installation fails, the package will be restored to its previous state.
Note that it is possible for the package installation to fail so badly that the lock directory is not removed: this inhibits any further installs to the library directory (or for--pkglock
, of the package) until the lock directory is removed manually.
也就是說阎曹,出于防止其他安裝過程干擾和暫存舊版本的目的,R在安裝X包時(shí)會(huì)先建立并鎖定一個(gè)叫00LOCK-X的臨時(shí)文件夾煞檩。安裝完畢后如果由于某種原因該臨時(shí)文件夾沒有被刪除的話处嫌,下次更新可能會(huì)因?yàn)殒i定失敗而gg。
這樣再回來看兩個(gè)方案就很容易理解了斟湃,方案一禁止安裝過程中鎖定文件夾熏迹,方案二從直接刪除lock文件夾的角度來說更干脆。