简体   繁体   English

如何为 R package 设置本地存储库?

[英]How to setup a local repository for R package?

I want to setup local repository for R package, I'd like the repository works like sonatype nexus(it can proxy the central repository, and cache the artifacts after downloading the artifact from central repository).我想为 R package 设置本地存储库,我希望存储库像 sonatype nexus 一样工作(它可以代理中央存储库,并在从中央存储库下载工件后缓存工件)。

Currently nexus does not support R repository format, so it doesn't suite what I needed to do.目前,nexus 不支持 R 存储库格式,因此它不适合我需要做的事情。

Is that any existing solution for creating this repository?这是创建此存储库的任何现有解决方案吗? I don't want to create a CRAN mirror, which is too heavy for me.我不想创建对我来说太重的 CRAN 镜像。

First, you'll want to make sure you have the following path and its directories in your system: "/R/src/contrib".首先,您需要确保系统中有以下路径及其目录:“/R/src/contrib”。 If you don't have this path and these directories, you'll need to create them.如果您没有此路径和这些目录,则需要创建它们。 All of your R packages files will be stored in the "contrib" directory.您所有的 R 包文件都将存储在“contrib”目录中。

Once you've added package files to the "contrib" directory, you can use the setRepositories function from the utils package to create the repository.将 package 文件添加到“contrib”目录后,您可以使用setRepositories工具 ZEFE90A8E604A7C840E88D03A67F6B7DZ 中的setRepositories function 来创建存储库。 I'd recommend adding the following code to your .Rprofile for a local repository:我建议将以下代码添加到本地存储库的.Rprofile中:

utils::setRepositories(ind = 0, addURLs = c(WORK = "file://<your higher directories>/R"))

ind = 0 will indicate that you only want the local repository. ind = 0表示您只想要本地存储库。 Additional repositories can be included in the addURLs = option and are comma separated within the character vector.附加存储库可以包含在addURLs =选项中,并在字符向量中以逗号分隔。

Next, create the repository index with the following code:接下来,使用以下代码创建存储库索引:

tools::write_PACKAGES("/<your higher directories>/R/src/contrib", verbose = TRUE) 

This will generate the PACKAGE files that serve as the repository index.这将生成用作存储库索引的 PACKAGE 文件。

To see what packages are in your repository, run the following code and take a look at the resulting data frame: my_packages <- available.packages()要查看存储库中有哪些包,请运行以下代码并查看生成的数据框: my_packages <- available.packages()

At this point, you can install packages from the repo without referencing the entire path to the package installation file.此时,您可以从 repo 安装包,而无需参考 package 安装文件的整个路径。 For example, to install the dplyr package, you could run the following:例如,要安装dplyr package,您可以运行以下命令:

install.packages("dplyr", dependencies = TRUE)

If you want to take it a step further and manage a changing repository, you could install and use the miniCRAN package.如果您想更进一步并管理不断变化的存储库,您可以安装和使用miniCRAN package。 Otherwise, you'll need to execute the write_PACKAGES function whenever your repository changes.否则,每当您的存储库更改时,您都需要执行write_PACKAGES function。

After installing the miniCRAN package, you can execute the following code to create the miniCRAN repo:安装miniCRAN package 后,您可以执行以下代码来创建miniCRAN 存储库:

 my_packages <- available.packages()

 miniCRAN::makeRepo(
  pkgs = my_packages[,1, 
  path = "/<your higher directories>/R",
  repos = getOption("repos"), 
  type = "source",
  Rversion = R.version, 
  download = TRUE, 
  writePACKAGES = TRUE,
  quiet = FALSE
 )

You only need to execute the code above once for each repo.您只需要为每个 repo 执行一次上面的代码。

Then, check to make sure each miniCRAN repo has been created.然后,检查以确保已创建每个 miniCRAN 存储库。 You only need to do this once for each repo:您只需为每个 repo 执行一次:

 pkgAvail(
  repos = getOption("repos"),
  type = "source",
  Rversion = R.version,
  quiet = FALSE
 )

Whenever new package files are placed into the local repo you can update the local repo's index as follows:每当将新的 package 文件放入本地存储库时,您可以按如下方式更新本地存储库的索引:

miniCRAN::updateRepoIndex("/<your higher directories>/R/")

Finally, as an optional step to see if the new package is in the index, create a data frame of the available packages and search the data frame:最后,作为查看新 package 是否在索引中的可选步骤,创建可用包的数据框并搜索数据框:

my_packages <- available.packages(repos = "file://<your higher directories>/R/")

This approach has worked pretty well for me, but perhaps others have comments and suggestions that could improve upon it.这种方法对我来说效果很好,但也许其他人有可以改进的意见和建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM