gpt4 book ai didi

r - Packrat 的 use.cache 功能是如何工作的?

转载 作者:行者123 更新时间:2023-12-04 18:58:39 25 4
gpt4 key购买 nike

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’
但它 如果包是 require,则加快您正在处理的 Packrat 项目的启动或 library在当前目录中调用。在这种情况下 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 中的软件包来缩短缓存选项的加载时间。这在安装大型软件包时可能会出现问题,例如 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
最简单的选择
在这些选项之间的某个地方可能最有意义 - 用户将他们的大型但常用包列表进行符号链接(symbolic link)(即 packrat::set_opts(external.packages=c('tidyverse', 'data.table')) ),然后忍受在一个项目的基础上安装较小的包。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com