R 中的依赖管理
R 是否有依赖管理工具来促进特定于项目的依赖关系?我正在寻找类似于Java的maven、Ruby的捆绑器、Python的virtualenv、Node的npm等的东西。
我知道描述文件中的“Depends”子句以及R_LIBS设施,但这些似乎并不协同工作,为一些非常常见的工作流程提供解决方案。
我本质上希望能够签出一个项目并运行单个命令来构建和测试该项目。该命令应将任何所需的包安装到特定于项目的库中,而不影响全局 R 安装。例如:
my_project/.Rlibs/*
Does R have a dependency management tool to facilitate project-specific dependencies? I'm looking for something akin to Java's maven, Ruby's bundler, Python's virtualenv, Node's npm, etc.
I'm aware of the "Depends" clause in the DESCRIPTION file, as well as the R_LIBS facility, but these don't seem to work in concert to provide a solution to some very common workflows.
I'd essentially like to be able to check out a project and run a single command to build and test the project. The command should install any required packages into a project-specific library without affecting the global R installation. E.g.:
my_project/.Rlibs/*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,您只能得到
DESCRIPTION:
文件中的Depends:
,原因如下:这是一个难题。
Unfortunately,
Depends:
within theDESCRIPTION:
file is all you get for the following reasons:This is a hard problem.
packrat
包正是为了实现以下目标:它允许在不同的项目本地包库中安装相同包的不同版本。
我添加这个答案,即使这个问题已经有 5 年历史了,因为这个解决方案在提出问题时显然还不存在(据我所知,
packrat
于 2014 年首次出现在 CRAN)。更新(2019 年 11 月)
新的 R 包
renv
替换了packrat
。The
packrat
package is precisely meant to achieve the following:It allows installing different versions of the same packages in different project-local package libraries.
I am adding this answer even though this question is 5 years old, because this solution apparently didn't exist yet at the time the question was asked (as far as I can tell,
packrat
first appeared on CRAN in 2014).Update (November 2019)
The new R package
renv
replacedpackrat
.作为权宜之计,我编写了一个新的 rbundler包裹。它将项目依赖项安装到特定于项目的子目录(例如
/.Rbundle
),从而允许用户避免使用全局库。我们已经在 Opower 使用
rbundler
几个月了,并且看到了开发人员工作流程的巨大改进,内部包的可测试性和可维护性。结合我们的内部包存储库,我们已经能够稳定开发十几个左右的包以用于生产应用程序。常见工作流程:
从 R 控制台:
<块引用>
库(rbundler)
捆绑('.')
所有依赖项都将安装到
./.Rbundle
中,并且将创建一个.Renviron
文件,其中包含以下内容:任何 R从此项目目录中运行的操作将遵循项目特定的库和包依赖项。请注意,虽然此方法使用包描述来定义依赖项,但它不需要具有实际的包结构。因此,rbundler 成为管理 R 项目的通用工具,无论它是简单的脚本还是成熟的包。
As a stop-gap, I've written a new rbundler package. It installs project dependencies into a project-specific subdirectory (e.g.
<PROJECT>/.Rbundle
), allowing the user to avoid using global libraries.We've been using
rbundler
at Opower for a few months now and have seen a huge improvement in developer workflow, testability, and maintainability of internal packages. Combined with our internal package repository, we have been able to stabilize development of a dozen or so packages for use in production applications.A common workflow:
From the R console:
All dependencies will be installed into
./.Rbundle
, and an.Renviron
file will be created with the following contents:Any R operations run from within this project directory will adhere to the project-speciic library and package dependencies. Note that, while this method uses the package DESCRIPTION to define dependencies, it needn't have an actual package structure. Thus,
rbundler
becomes a general tool for managing an R project, whether it be a simple script or a full-blown package.您可以使用以下工作流程:
1)创建一个脚本文件,其中包含您想要设置的所有内容并将其存储在您的项目目录中,例如projectInit.R
2)从您的.Rprofile(或由R执行的任何其他文件)获取此脚本在启动时)使用 try 语句
这将保证即使没有找到projectInit.R,R启动时也不会出现错误消息
3)如果您在项目目录中启动R,则将获取projectInit.R文件(如果目录中存在)并且你准备好了go
这是从 Linux 角度来看的,但在 Windows 和 Mac 下也应该以相同的方式工作。
You could use the following workflow:
1) create a script file, which contains everything you want to setup and store it in your projectd directory as e.g. projectInit.R
2) source this script from your .Rprofile (or any other file executed by R at startup) with a try statement
This will guarantee that even when no projectInit.R is found, R starts without error message
3) if you start R in your project directory, the projectInit.R file will be sourced if present in the directory and you are ready to go
This is from a Linux perspective, but should work in the same way under windows and Mac as well.