参见:https://zhuanlan.zhihu.com/p/146355865[1]
与别人分享代码时,常常会因为不同人机器的R与R 包的版本差异,而导致了出现报错。
R 版本比较好控制,直接官网下一个就好了,可是各种R 包纷繁错乱,而且版本更是各有不同,如何能控制呢?
主要介绍packrat,来解决它。
packrat 可以帮助我们管理和R 包的相关需求,包括:
我们直接通过install 即可安装。
packrat::init()
我们可以对init 指定目录,如果不指定,则是在工作环境的默认目录下创建(getwd查看)一个属于packrat 的目录,其会自动下载并保存当前工作环境中已经加载的R 包到packrat 目录下:
> packrat::init()
Initializing packrat project in directory:
- "D:/R测试"
Adding these packages to packrat:
_
BH 1.75.0-0
BiocManager 1.30.10
R6 2.3.0
这时候我们也会进入packrat 模式,此时包的读取都会在packrat 的目录中,而非本身R 包的目录,如果想要加载进入packrat 模式前未曾加载的包,则其会进行重新安装:
> .libPaths()
[1] "D:/01-工作数据集/Rtest/packrat/lib/x86_64-w64-mingw32/4.0.2"
[2] "D:/01-工作数据集/Rtest/packrat/lib-ext/x86_64-w64-mingw32/4.0.2"
[3] "D:/01-工作数据集/Rtest/packrat/lib-R/x86_64-w64-mingw32/4.0.2"
> pacman::p_load(conflicted) # 包名冲突报警
Installing package into ‘D:/01-工作数据集/Rtest/packrat/lib/x86_64-w64-mingw32/4.0.2’
(as ‘lib’ is unspecified)
可见,是实打实的装了包在里面:
我们在用R 更新或安装包的可能会遇到各种依赖导致的问题,可以使用snapshot 在对包环境修改前进行保存:
packrat::snapshot()
如果发现对R 包操作后产生的意外不满意,可以使用restore 恢复,不过我这里发现一个小bug,即便我用clean 清楚掉了packrat 中的所有包,其还是显示:
> packrat::restore()
Already up to date.
当我们最终确定好了项目执行所需要的全部包,可以将其打包:
packrat::bundle()
但发现这里使用unbundle 后,还是发生报错(这个bug 先留个坑):
Error in normalizePath(bundle, winslash = "/", mustWork = TRUE) :
缺少参数"bundle",也没有缺省值packrat::unbundle(bundle = "../Rtest/packrat/bundles/Rtest-2021-04-19.tar.gz", where = ".") # 正确代码应该长这样
其他的一些操作包括get_opt 与set_opt 修改一些packrat 的配置:
> packrat::get_opts()
$auto.snapshot
[1] FALSE
$use.cache
[1] FALSE
$print.banner.on.startup
[1] "auto"
$vcs.ignore.lib
[1] TRUE
$vcs.ignore.src
[1] FALSE
$external.packages
character(0)
$local.repos
character(0)
$load.external.packages.on.startup
[1] TRUE
$ignored.packages
character(0)
$ignored.directories
[1] "data" "inst"
$quiet.package.installation
[1] TRUE
$snapshot.recommended.packages
[1] FALSE
$snapshot.fields
[1] "Imports" "Depends" "LinkingTo"
$symlink.system.packages
[1] TRUE
还有其他一些操作,可以创建一个用户自己的R 包分支:
Manage ad-hoc local repositories (note that these are a separate entity from CRAN-like repositories):
packrat::set_opts(local.repos = ...) can be used to specify local repositories; that is, directories containing (unzipped) package sources.
packrat::install_local() installs packages available in a local repository.
For example, suppose I have the (unzipped) package sources for digest located within the folder~/git/R/digest/. To install this package, you can use:
packrat::set_opts(local.repos = "~/git/R")
packrat::install_local("digest")
There are also utility functions for using and managing packages in the external / user library, and can be useful for leveraging packages in the user library that you might not want as project-specific dependencies, e.g. devtools, knitr, roxygen2:
packrat::extlib(): Load an external package.
packrat::with_extlib(): With an external package, evaluate an expression. The external package is loaded only for the duration of the evaluated expression, but note that there may be other side effects associated with the package's .onLoad, .onAttach and .onUnload calls that we may not be able to fully control.
ps:忽然感觉,packrat 和pacman,都是pac 打头的,hhh。
通常来说的操作,先前的init,以及最后snapshot 之后,就可以bundle 自己的包了。
如果需要退出packrat 模式回到本来的包环境,直接off 一下退出即可:
packrat::off()
后面探索了一下,发现unbundle 这个函数的正确使用方法了:
packrat::unbundle(bundle = "../Rtest/packrat/bundles/Rtest-2021-04-19.tar.gz",
where = ".")
其中重要的两个参数bundle 指定压缩包所在的位置,where 用来选择需要存放该项目的路径。
惊讶的发现,它不仅打包了包,还打包了全部的代码。
现在市面上那些提供代码的文章,能力可行的情况下,直接用packrat 打包会不会更好,更便于传播?这其实也是一个开发者与使用者谁更方便的tradeoff。
直接init 创建新的环境,用snapshot 保存,接着操作,如果出问题就restore 回去。退出用off。
完成全部代码后,确保没问题后,init 创建,bundle 打包,off 退出。如果打包前不放心,可以用status 函数检查:
> packrat::status()
The following packages are referenced in your code, but are not present
in your library nor in packrat:
beepr
You will need to install these packages manually, then use
packrat::snapshot() to record these packages in packrat.
Warning message:
In FUN(X[[i]], ...) :
Package 'beepr' not available in repository or locally
别的电脑接受bundle 的压缩包后,在非R project 下,用unbundle 打开该包:
进入环境后,直接on 就可以操作了。
参见:https://www.rdocumentation.org/packages/checkpoint/versions/0.4.10[2]
checkpoint 的使用就比较简单了:
checkpoint("2021-04-10")
它会从上述指定的日期获得快照。
checkpoint可以:
创建和使用的代码是一样的。快照信息存在~/.checkpoint 但目前还是没有明白,其是如何读取或者与默认R 包安装目录相分离的。
我们可以直接通过docker 获得别人的R 环境。
只是相比R 包管理,docker 的学习成本大了一些。
可以参考下面这个文章:
惊艳 | RStuido server选择不同的R版本(conda中的不同R版本) - 云+社区 - 腾讯云 (tencent.com)[3]
但个人觉得conda 的缺点在于,会新安装一整套R 和R 的依赖,相对来说有点浪费空间。
[1]https://zhuanlan.zhihu.com/p/146355865: https://zhuanlan.zhihu.com/p/146355865
[2]https://www.rdocumentation.org/packages/checkpoint/versions/0.4.10: https://www.rdocumentation.org/packages/checkpoint/versions/0.4.10
[3]惊艳 | RStuido server选择不同的R版本(conda中的不同R版本) - 云+社区 - 腾讯云 (tencent.com): https://cloud.tencent.com/developer/article/1845582