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
如何运作?
答案 0 :(得分:5)
使用以下命令使用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
基本库和已安装的库都存储在缓存和符号链接中:
./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
如果您尝试安装软件包,它会覆盖符号链接,而不是从缓存中获取软件包,因此无法用于加快软件包的安装。
>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’
但如果软件包在当前目录中进行require
或library
调用,会加快启动您正在处理的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中的软件包来缩短加载时间和缓存选项。从源代码安装大型软件包(例如tidyverse
)时可能会出现问题(就像在Linux系统上一样)。
有几种解决方法:
一个简单的解决方法是将用户包库符号链接到空的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)
Packrat确实为使用packrat::set_opts(external.packages=c('pkgname'))
命令的大型软件包提供了一种解决方法,但以这种方式安装的软件包不包含在packrat / src文件夹中。
实际上,该选项将包目录符号链接到packrat/lib-ext
diretory。
我采用与symlinking选项相同的方式自动执行此操作,以获取其主目录中的所有用户包并将其添加到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'))
),然后在项目的基础上逐项安装较小的包。