创建本地 R 包存储库
我想创建一个本地 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,CRAN 的副本或带有本地软件包的存储库都很容易设置。想必您希望在 Windows 上这样做,所以这样做:
R/
R/bin/windows/contrib/2.11< /代码>。如果您需要支持其他(早期)版本,只需在
2.11
目录旁边创建目录2.10
、2.9
、... 即可。将所需的包放入目录(例如
2.11
)中,然后切换到该目录并运行以下命令来生成PACKAGES
和PACKAGES。存储库的 .gz
文件:tools::write_PACKAGES(".", type="win.binary")
这就是全部内容 - 现在您可以通过指向给定命令的地址来访问存储库,就像
我在 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:
R/
R/bin/windows/contrib/2.11
. If you need to support other (earlier) releases, simply create directories2.10
,2.9
, ... next to the2.11
directory.Place the packages you need into the directory (say,
2.11
), then change into that directory and run the following command to generatePACKAGES
andPACKAGES.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
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).
阅读管理员指南的部分。
Read the section of the Administrator guide.
包 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.
我也无法访问 CRAN 镜像来安装软件包。因此,我发现这些步骤很有帮助。
首先,您需要确保系统中具有以下路径及其目录:“/R/src/contrib”。如果您没有此路径和这些目录,则需要创建它们。所有 R 包文件都将存储在“contrib”目录中。
将包文件添加到“contrib”目录后,您可以使用 utils 包中的
setRepositories
函数来创建存储库。我建议将以下代码添加到本地存储库的 .Rprofile 中:编辑 .Rprofile 后,重新启动 R。
ind = 0
将指示您只需要本地存储库。其他存储库可以包含在addURLs =
选项中,并在字符向量中以逗号分隔。接下来,使用以下代码创建存储库索引:
这将生成用作存储库索引的 PACKAGE 文件。
要查看存储库中有哪些软件包,请运行以下代码并查看生成的数据框:
my_packages <- available.packages()
此时,您可以从存储库安装软件包无需引用包安装文件的完整路径。例如,要安装dplyr软件包,您可以运行以下命令:
如果您想更进一步并管理不断变化的存储库,您可以安装并使用miniCRAN > 包。否则,每当您的存储库发生更改时,您都需要执行
write_PACKAGES
函数。安装miniCRAN软件包后,您可以执行以下代码来创建miniCRAN存储库:
您只需为每个存储库执行一次上述代码。
然后,检查以确保每个 miniCRAN 存储库均已创建。您只需为每个存储库执行一次此操作:
每当将新包文件放入本地存储库时,您都可以按如下方式更新本地存储库的索引:
最后,作为查看新包是否在索引中的可选步骤,创建一个可用包的数据框架并搜索数据框架:
这种方法对我来说效果很好,但也许其他人有可以改进它的评论和建议。
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:After editing your .Rprofile, restart R.
ind = 0
will indicate that you only want the local repository. Additional repositories can be included in theaddURLs =
option and are comma separated within the character vector.Next, create the repository index with the following code:
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:
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:
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:
Whenever new package files are placed into the local repo you can update the local repo's index as follows:
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:
This approach has worked pretty well for me, but perhaps others have comments and suggestions that could improve upon it.