你如何使用同一个 R 包的多个版本? [英] How do you use multiple versions of the same R package?

查看:24
本文介绍了你如何使用同一个 R 包的多个版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了能够比较一个包的两个版本,我需要能够选择我加载哪个版本的包.R 的包系统默认设置为覆盖现有包,因此您始终拥有最新版本.如何覆盖此行为?

In order to be able to compare two versions of a package, I need to able to choose which version of the package that I load. R's package system is set to by default to overwrite existing packages, so that you always have the latest version. How do I override this behaviour?

到目前为止,我的想法是:

My thoughts so far are:

我可以获取包源,编辑描述以提供不同的名称,并实际上构建两个不同的包.不过,我宁愿能够直接使用二进制文件,因为这样更省事.

I could get the package sources, edit the descriptions to give different names and build, in effect, two different packages. I'd rather be able to work directly with the binaries though, as it is much less hassle.

我不一定需要同时加载两个版本的软件包(只是同时安装在某个地方).我可能会弄乱 Sys.getenv('R_HOME') 来更改 R 安装软件包的位置,然后 .libpaths() 来更改 R 的位置寻找他们.不过,这似乎很hacky,那么有人有更好的想法吗?

I don't necessarily need to have both versions of the packages loaded at the same time (just installed somewhere at the same time). I could perhaps mess about with Sys.getenv('R_HOME') to change the place where R installs the packages, and then .libpaths() to change the place where R looks for them. This seems hacky though, so does anyone have any better ideas?

推荐答案

您可以选择性地更改库路径.为了完全透明,请将两者都放在您通常的路径之外,然后再做

You could selectively alter the library path. For complete transparency, keep both out of your usual path and then do

 library(foo, lib.loc="~/dev/foo/v1")    ## loads v1

 library(foo, lib.loc="~/dev/foo/v2")    ## loads v2

当然,install.packages() 也是如此.所有这些命令都有许多参数,因此您所针对的钩子可能已经存在.所以不要看更改R_HOME,而是看help(install.packages)(假设你从源代码安装).

The same works for install.packages(), of course. All these commands have a number of arguments, so the hooks you aim for may already be present. So don't look at changing R_HOME, rather look at help(install.packages) (assuming you install from source).

但是 AFAIK 你不能以相同的名称加载同一个包两次.

But AFAIK you cannot load the same package twice under the same name.

这篇关于你如何使用同一个 R 包的多个版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆