renv 和 git 交互 R

renv and git interaction R

我试图更好地理解 renv R 中的包如何工作以及它如何与 git 交互。这是我的问题

  1. 假设我在我的 R 项目中有 master 和几个 git 分支(master 和 branches)我想使用不同的环境(不同的库或相同的不同版本库)。 renv 是否能够处理它,即如果我从一个分支切换到另一个分支将需要调用 renv::restore().

  2. 我有两个单独的项目,其中都有 renv 运行,称它们为项目 A 和项目 B。我想从项目 B 获取环境并替换项目A中的环境。我怎样才能完成它?我只需要将 renv 文件夹从一个项目复制到另一个项目吗?

Assume I have master and a couple git branches in my R projects for each (master and branches) I would like to use different environments (different libraries or different versions of the same libraries). Would renv be able to handle it, i.e. if I switch from one branch to another will need to call renv::restore().

renv 将项目库从版本控制中排除(以避免膨胀存储库大小),因此通常在切换分支时需要恢复库路径。

这有点困难,所以另一种解决方案是配置 renv 为您的 git 存储库的每个分支使用不同的库路径。你可以用类似的东西来完成这个(在你的项目 .Rprofile):

branch <- system("git rev-parse --abbrev-ref HEAD", intern = TRUE)
Sys.setenv(RENV_PATHS_LIBRARY = file.path("renv/library/branches", branch))

这样,renv 将自动配置为每个分支使用单独的库,并且这会在您切换分支时自动发生。

总的来说,这似乎是有用的东西;如果是这样,您可以考虑在 https://github.com/rstudio/renv/issues 提交功能请求。

I have two separate projects with renv running in both of them, call them project A and project B. I would like to take environment from project B and replace environment in project A. How can I accomplish it? Do I just need to copy renv folder from one project to another?

这应该足够了,尽管这取决于您要复制多少环境。 renv 文件夹包含一个 settings.dcf 文件,用于定义项目设置——您可能也可能不想复制这些设置。 (有关 renv 的项目特定设置的文档,请参阅 ?renv::settings。)

或者,您可以将项目 renv.lock 从项目 B 复制到项目 A,然后调用 renv::restore()。如果你是,这可能更合适。将项目从一台机器复制到另一台机器,特别是如果这些机器 运行 在不同的操作系统上。