创建本地 R 包存储库

发布于 2024-09-02 17:08:53 字数 107 浏览 5 评论 0原文

我想创建一个本地 R 软件包存储库,以便我公司的用户可以从中安装软件包,并且系统管理员可以定期更新本地存储库。目前对 CRAN 镜像的访问被拒绝。

有没有一种简单的方法可以做到这一点?

I would like to create a local R package repository such that users in my company can install packages from it and the system admins can update the local repo periodically. Access to the CRAN mirrors is currently denied.

Is there a simple way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

少跟Wǒ拽 2024-09-09 17:08:53

是的,CRAN 的副本或带有本地软件包的存储库都很容易设置。想必您希望在 Windows 上这样做,所以这样做:

  1. 在您的网络服务器上创建一个顶级目录,例如 R/
  2. 在其中创建常用的层次结构:R/bin/windows/contrib/2.11< /代码>。如果您需要支持其他(早期)版本,只需在 2.11 目录旁边创建目录 2.102.9、... 即可。
  3. 将所需的包放入目录(例如 2.11)中,然后切换到该目录并运行以下命令来生成 PACKAGESPACKAGES。存储库的 .gz 文件:

    tools::write_PACKAGES(".", type="win.binary")

这就是全部内容 - 现在您可以通过指向给定命令的地址来访问存储库,就像

update.packages(repos="http://my.local.server/R", ask=FALSE)

我在 R/zzz.R 中所做的那样 用于本地软件包,以便它们自行更新。

大约五年多后进行编辑drat 包现在可以自动化很多这样的内容,如果您还使用 GitHub 通过 http/https 提供存储库(但对于其他或本地托管也很有用),那么尤其如此。

Yes, either a copy of CRAN or a repo with local packages is easy to set up. Presumably you want this for Windows so do this:

  1. Create a top-level directory on your webserver, say R/
  2. Create the usual hierarchy in there: R/bin/windows/contrib/2.11. If you need to support other (earlier) releases, simply create directories 2.10, 2.9, ... next to the 2.11 directory.
  3. Place the packages you need into the directory (say, 2.11), then change into that directory and run the following command to generate PACKAGES and PACKAGES.gz files for the repository:

    tools::write_PACKAGES(".", type="win.binary")

That is all there is to it -- now you can access the repository by pointing to the address given a command such as

update.packages(repos="http://my.local.server/R", ask=FALSE)

which I even do in R/zzz.R for local packages so that they update themselves.

Edit some five+ years later: And the drat package now automates a lot of this, and shines particularly if you also use GitHub to serve the repository over http/https (but is useful for other or local hosting too).

机场等船 2024-09-09 17:08:53

阅读管理员指南的部分

桃扇骨 2024-09-09 17:08:53

miniCRAN 也为此提供了强大的功能。关键优点是您不需要完整的镜像,但可以仅使用您需要的软件包发行版(包括它们的依赖项)来设置 CRAN 的“迷你”镜像。

The package miniCRAN also provides great functionality for this. The key advantage being that you don't need a full mirror, but can setup a "mini" mirror of CRAN with only the packages distributions you need, including their dependencies.

睡美人的小仙女 2024-09-09 17:08:53

我也无法访问 CRAN 镜像来安装软件包。因此,我发现这些步骤很有帮助。

首先,您需要确保系统中具有以下路径及其目录:“/R/src/contrib”。如果您没有此路径和这些目录,则需要创建它们。所有 R 包文件都将存储在“contrib”目录中。

将包文件添加到“contrib”目录后,您可以使用 utils 包中的 setRepositories 函数来创建存储库。我建议将以下代码添加到本地存储库的 .Rprofile 中:

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

编辑 .Rprofile 后,重新启动 R。

ind = 0 将指示您只需要本地存储库。其他存储库可以包含在 addURLs = 选项中,并在字符向量中以逗号分隔。

接下来,使用以下代码创建存储库索引:

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

这将生成用作存储库索引的 PACKAGE 文件。

要查看存储库中有哪些软件包,请运行以下代码并查看生成的数据框: my_packages <- available.packages()

此时,您可以从存储库安装软件包无需引用包安装文件的完整路径。例如,要安装dplyr软件包,您可以运行以下命令:

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

如果您想更进一步并管理不断变化的存储库,您可以安装并使用miniCRAN > 包。否则,每当您的存储库发生更改时,您都需要执行 write_PACKAGES 函数。

安装miniCRAN软件包后,您可以执行以下代码来创建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
 )

您只需为每个存储库执行一次上述代码。

然后,检查以确保每个 miniCRAN 存储库均已创建。您只需为每个存储库执行一次此操作:

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

每当将新包文件放入本地存储库时,您都可以按如下方式更新本地存储库的索引:

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

最后,作为查看新包是否在索引中的可选步骤,创建一个可用包的数据框架并搜索数据框架:

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

这种方法对我来说效果很好,但也许其他人有可以改进它的评论和建议。

I also don't have access to CRAN mirrors for package installation. As a result, these are some steps I've found to be helpful.

First, you'll want to make sure you have the following path and its directories in your system: "/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.

Once you've added package files to the "contrib" directory, you can use the setRepositories function from the utils package to create the repository. I'd recommend adding the following code to your .Rprofile for a local repository:

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

After editing your .Rprofile, restart R.

ind = 0 will indicate that you only want the local repository. Additional repositories can be included in the addURLs = option and are comma separated within the character vector.

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.

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()

At this point, you can install packages from the repo without referencing the entire path to the package installation file. For example, to install the dplyr package, you could run the following:

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. Otherwise, you'll need to execute the write_PACKAGES function whenever your repository changes.

After installing the miniCRAN package, you can execute the following code to create the miniCRAN repo:

 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.

Then, check to make sure each miniCRAN repo has been created. You only need to do this once for each 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:

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:

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.

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