美文网首页
R语言ERROR解读|failed to lock direct

R语言ERROR解读|failed to lock direct

作者: 云养江停 | 来源:发表于2023-01-11 15:38 被阅读0次
    image.png

    今天升级rlang的时候报了错:

    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
    
    1673422652510.png

    这时如果查看对应安装包的文件夹(例子中是win-library/4.0),会发现多了一个叫做“00LOCK-rlang”(或者直接叫“00LOCK”)的文件夹。

    先放解决方案:

    方案1:install.packages() 加上INSTALL_opts = '--no-lock':

    install.packages("your_package", INSTALL_opts = '--no-lock')
    

    方案1会安装升级成功,但是00LOCK-rlang文件夹还在——说明下次更新此包时仍可能出同样的error。

    方案2:use unlink() to delete 00LOCK-rlang

    unlink("C:/path_to_your_pkgs/00LOCK-rlang", recursive = TRUE)
    

    删除00LOCK-rlang文件夹,后续照常安装即可。如果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 directory 00LOCK 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 when Ncpus > 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 argument lock to TRUE (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 with libs_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包时会先建立并锁定一个叫00LOCK-X的临时文件夹。安装完毕后如果由于某种原因该临时文件夹没有被删除的话,下次更新可能会因为锁定失败而gg。

    这样再回来看两个方案就很容易理解了,方案一禁止安装过程中锁定文件夹,方案二从直接删除lock文件夹的角度来说更干脆。

    参考:

    1. RDocumentation: install.packages

    2. stackoverflow: R install.packages returns “failed to create lock directory”

    3. https://zhuanlan.zhihu.com/p/264363714

    相关文章

      网友评论

          本文标题:R语言ERROR解读|failed to lock direct

          本文链接:https://www.haomeiwen.com/subject/udsvcdtx.html