R 中的依赖管理

发布于 2024-12-09 07:11:21 字数 286 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

两人的回忆 2024-12-16 07:11:21

不幸的是,您只能得到 DESCRIPTION: 文件中的 Depends:,原因如下:

  • R 本身是相当跨平台的,但这意味着我们需要它能够跨平台工作和操作系统
  • 编码依赖项:除了 R 包之外,还需要以可移植的方式跨操作系统对依赖项进行编码——祝您好运,即使是简单的东西(例如“PNG 图形库”)也可以以可解析的方式进行编码明确地跨系统
  • Windows 没有软件包管理器
  • AFAIK OS X 没有一个软件包管理器来混合 Apple 提供的内容和其他开源项目提供的内容
  • 即使在 Linux 发行版中,您也无法获得一致性:仅以 RStudio 为例,它有两个软件包(都提供它们的依赖关系!)对于 RedHat/Fedora 和 Debian/Ubuntu

这是一个难题。

Unfortunately, Depends: within the DESCRIPTION: file is all you get for the following reasons:

  • R itself is reasonably cross-platform, but that means we need this to work across platforms and OSs
  • Encoding Depends: beyond R packages requires encoding the Depends in a portable manner across operating systems---good luck encoding even something simple such as 'a PNG graphics library' in a way that can be resolved unambiguously across systems
  • Windows does not have a package manager
  • AFAIK OS X does not have a package manager that mixes what Apple ships and what other Open Source projects provide
  • Even among Linux distributions, you do not get consistency: just take RStudio as an example which comes in two packages (which all provide their dependencies!) for RedHat/Fedora and Debian/Ubuntu

This is a hard problem.

送舟行 2024-12-16 07:11:21

packrat 包正是为了实现以下目标:

将任何所需的包安装到特定于项目的库中,而不影响全局 R 安装

它允许在不同的项目本地包库中安装相同包的不同版本。

我添加这个答案,即使这个问题已经有 5 年历史了,因为这个解决方案在提出问题时显然还不存在(据我所知,packrat 于 2014 年首次出现在 CRAN)。

更新(2019 年 11 月)

新的 R 包 renv 替换了 packrat

The packrat package is precisely meant to achieve the following:

install any required packages into a project-specific library without affecting the global R installation

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 replaced packrat.

半寸时光 2024-12-16 07:11:21

作为权宜之计,我编写了一个新的 rbundler包裹。它将项目依赖项安装到特定于项目的子目录(例如/.Rbundle),从而允许用户避免使用全局库。

我们已经在 Opower 使用 rbundler 几个月了,并且看到了开发人员工作流程的巨大改进,内部包的可测试性和可维护性。结合我们的内部包存储库,我们已经能够稳定开发十几个左右的包以用于生产应用程序。

常见工作流程:

  • 从 github 检出项目
  • cd 进入项目目录
  • 启动 R
  • 从 R 控制台:

    <块引用>

    库(rbundler)

    捆绑('.')

所有依赖项都将安装到 ./.Rbundle 中,并且将创建一个 .Renviron 文件,其中包含以下内容:

R_LIBS_USER='.Rbundle'

任何 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:

  • Check out a project from github
  • cd into the project directory
  • Fire up R
  • From the R console:

    library(rbundler)

    bundle('.')

All dependencies will be installed into ./.Rbundle, and an .Renviron file will be created with the following contents:

R_LIBS_USER='.Rbundle'

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.

夜巴黎 2024-12-16 07:11:21

您可以使用以下工作流程:

1)创建一个脚本文件,其中包含您想要设置的所有内容并将其存储在您的项目目录中,例如projectInit.R

2)从您的.Rprofile(或由R执行的任何其他文件)获取此脚本在启动时)使用 try 语句

try(source("./projectInit.R"), silent=TRUE)

这将保证即使没有找到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

try(source("./projectInit.R"), silent=TRUE)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文