Packrat具有use.cache功能以减少软件包安装时间。

该文档提供以下信息:
use.cache: Install packages into a global cache, which is then shared across projects? The directory to use is read through Sys.getenv("R_PACKRAT_CACHE_DIR"). Not yet implemented for Windows. (logical; defaults to FALSE)
但是,运行install.package()不会从用户库中获取已安装的软件包。

use.cache如何工作?

最佳答案

在启用全局缓存的情况下进行安装
使用以下命令使用packrat设置缓存:

#Optional to set location of cache:
#Sys.setenv(R_PACKRAT_CACHE_DIR = "/home/willbowditch/R/packratcache")

packrat::set_opts(use.cache=TRUE)
这被写入packrat.opts,它决定在Rstudio中打开项目时是否使用缓存。
auto.snapshot: TRUE
use.cache: TRUE
print.banner.on.startup: auto
vcs.ignore.lib: TRUE
vcs.ignore.src: FALSE
external.packages:
local.repos:
load.external.packages.on.startup: TRUE
ignored.packages:
quiet.package.installation: TRUE
snapshot.recommended.packages: FALSE
snapshot.fields:
    Imports
    Depends
    LinkingTo
基本库和已安装库都存储在缓存中并进行符号链接(symbolic link):
./packrat/lib/x86_64-pc-linux-gnu/3.4.0:
total 2
drwxr-xr-x 2 willbowditch staff  4 Jun 14 16:21 .
drwxr-xr-x 3 willbowditch staff  3 Jun 14 16:20 ..
lrwxrwxrwx 1 willbowditch staff 99 Jun 14 16:21 CheckDigit -> /home/willbowditch/R/packratcache/v2/library/CheckDigit/0ab3083cafb11382646fdda41ddb8b98/CheckDigit
lrwxrwxrwx 1 willbowditch staff 93 Jun 14 16:21 packrat -> /home/willbowditch/R/packratcache/v2/library/packrat/6ad605ba7b4b476d84be6632393f5765/packrat

./packrat/lib-ext:
total 9
drwxr-xr-x 2 willbowditch staff 2 Jun 14 16:20 .
drwxr-xr-x 6 willbowditch staff 9 Jun 14 16:20 ..

./packrat/lib-R:
total 24
drwxr-xr-x 2 willbowditch staff 16 Jun 14 16:20 .
drwxr-xr-x 6 willbowditch staff  9 Jun 14 16:20 ..
lrwxrwxrwx 1 willbowditch staff 29 Jun 14 16:20 base -> /usr/local/lib/R/library/base
lrwxrwxrwx 1 willbowditch staff 33 Jun 14 16:20 compiler -> /usr/local/lib/R/library/compiler
lrwxrwxrwx 1 willbowditch staff 33 Jun 14 16:20 datasets -> /usr/local/lib/R/library/datasets
lrwxrwxrwx 1 willbowditch staff 33 Jun 14 16:20 graphics -> /usr/local/lib/R/library/graphics
lrwxrwxrwx 1 willbowditch staff 34 Jun 14 16:20 grDevices -> /usr/local/lib/R/library/grDevices
lrwxrwxrwx 1 willbowditch staff 29 Jun 14 16:20 grid -> /usr/local/lib/R/library/grid
lrwxrwxrwx 1 willbowditch staff 32 Jun 14 16:20 methods -> /usr/local/lib/R/library/methods
lrwxrwxrwx 1 willbowditch staff 33 Jun 14 16:20 parallel -> /usr/local/lib/R/library/parallel
lrwxrwxrwx 1 willbowditch staff 32 Jun 14 16:20 splines -> /usr/local/lib/R/library/splines
lrwxrwxrwx 1 willbowditch staff 30 Jun 14 16:20 stats -> /usr/local/lib/R/library/stats
lrwxrwxrwx 1 willbowditch staff 31 Jun 14 16:20 stats4 -> /usr/local/lib/R/library/stats4
lrwxrwxrwx 1 willbowditch staff 30 Jun 14 16:20 tcltk -> /usr/local/lib/R/library/tcltk
lrwxrwxrwx 1 willbowditch staff 30 Jun 14 16:20 tools -> /usr/local/lib/R/library/tools
lrwxrwxrwx 1 willbowditch staff 30 Jun 14 16:20 utils -> /usr/local/lib/R/library/utils
如果尝试安装软件包,它将覆盖符号链接(symbolic link),而不是从缓存中获取软件包,因此不能使用它来加快软件包的安装速度。
>install.packages('CheckDigit')
Installing package into ‘/home/willbowditch/packrattest/packrat/lib/x86_64-pc-linux-gnu/3.4.0’
(as ‘lib’ is unspecified)
trying URL 'https://mran.microsoft.com/snapshot/2017-06-07/src/contrib/CheckDigit_0.1-1.tar.gz'
Content type 'application/octet-stream' length 3777 bytes
==================================================
downloaded 3777 bytes

* installing *source* package ‘CheckDigit’ ...
** package ‘CheckDigit’ successfully unpacked and MD5 sums checked
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (CheckDigit)

The downloaded source packages are in
    ‘/tmp/RtmpxAU8pv/downloaded_packages’
但是,如果软件包在当前目录中是requirelibrary调用,它确实会加快启动正在处理的packrat项目的速度。在这种情况下,packrat::init()packrat::restore()从缓存中恢复软件包,但前提是之前这些软件包已经在启用了缓存的Packrat项目中使用过。
> packrat::init()
Initializing packrat project in directory:
- "~/six"
Fetching sources for BH (1.62.0-1) ... OK (CRAN current)
Fetching sources for DBI (0.6-1) ... OK (CRAN current)
Fetching sources for R6 (2.2.0) ... OK (CRAN current)
Fetching sources for Rcpp (0.12.10) ... OK (CRAN current)
Fetching sources for assertthat (0.2.0) ... OK (CRAN current)
Fetching sources for dplyr (0.5.0) ... OK (CRAN current)
Fetching sources for lazyeval (0.2.0) ... OK (CRAN current)
Fetching sources for magrittr (1.5) ... OK (CRAN current)
Fetching sources for packrat (0.4.8-1) ... OK (CRAN current)
Fetching sources for stringi (1.1.5) ... OK (CRAN current)
Fetching sources for tibble (1.3.0) ... OK (CRAN current)
Fetching sources for tidyr (0.6.2) ... OK (CRAN current)
Fetching sources for whisker (0.3-2) ... OK (CRAN current)
Snapshot written to '/home/willbowditch/six/packrat/packrat.lock'
Installing BH (1.62.0-1) ...
    OK (symlinked cache)
Installing DBI (0.6-1) ...
    OK (symlinked cache)
Installing R6 (2.2.0) ...
    OK (symlinked cache)
Installing Rcpp (0.12.10) ...
    OK (symlinked cache)
Installing assertthat (0.2.0) ...
    OK (symlinked cache)
Installing lazyeval (0.2.0) ...
    OK (symlinked cache)
Installing magrittr (1.5) ...
    OK (symlinked cache)
Installing packrat (0.4.8-1) ...
    OK (symlinked cache)
Installing stringi (1.1.5) ...
    OK (symlinked cache)
Installing whisker (0.3-2) ...
    OK (symlinked cache)
Installing tibble (1.3.0) ...
    OK (symlinked cache)
Installing dplyr (0.5.0) ...
    OK (symlinked cache)
Installing tidyr (0.6.2) ...
    OK (symlinked cache)
Initialization complete!
换句话说,软件包似乎并不从全局库传递到高速缓存,但是它们可以从其他packrat库传递到高速缓存。
从用户主页(〜)库快速将软件包安装到Packrat项目
据我所知,您不能使用packrat中尚未安装的软件包来通过cache选项缩短加载时间。从源安装大型软件包(例如tidyverse)时,这可能是个问题(在Linux系统上必须如此)。
有两种解决方法:
解决方法1:符号链接(symbolic link)您的库
一个简单的解决方法是将用户软件包库符号链接(symbolic link)到一个空的packrat目录。通过这种方法的安装时间只有几秒钟,而且只要在开发结束时运行packrat::clean(),它似乎就不会干扰创建快照的过程。
脚步
新项目>使用packrat
source('https://raw.githubusercontent.com/willbowditch/ratpack/master/R/ratpack.R')
symlink_packages()
#Develop as normal then run
packrat::clean()
packrat::snapshot(ignore.stale=TRUE)
解决方法2:external.packages
Packrat确实使用packrat::set_opts(external.packages=c('pkgname'))命令为大型软件包提供了一种解决方法,但是以这种方式安装的软件包未包含在packrat/src文件夹中。
实际上,该选项将软件包目录符号链接(symbolic link)到packrat/lib-ext目录。
我可以用与符号链接(symbolic link)选项相同的方式来实现这一点的自动化-捕获其主目录中的所有用户软件包并将其添加到external.packages选项中。
脚步
新项目>使用packrat
source('https://raw.githubusercontent.com/willbowditch/ratpack/master/R/ratpack.R')
   import_user_packages()
   #All installed packages will now be accessable within the packrat session
在开发结束时重置
   packrat::set_opts(external.packages=NULL)
   packrat::snapshot()
   packrat::restore() #This step will install the packages if they're not in the cache
最简单的选择
在这些选项之间的某个位置可能最有意义-用户整理要链接的大型但常用软件包的列表(即packrat::set_opts(external.packages=c('tidyverse', 'data.table'))),然后忍受在逐个项目的基础上安装较小的软件包。

关于r - Packrat的use.cache功能如何工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44676570/

10-12 20:35