安装所有尚未安装的 CRAN 软件包?

发布于 2024-08-19 08:53:38 字数 296 浏览 4 评论 0原文

以下 R 命令将安装所有 CRAN 软件包:

availablePackages <- available.packages()[,1]
install.packages(availablePackages)

以下命令将列出所有已安装的软件包:

installedPackages <- .packages(all.available = TRUE)

我的问题是:如何指示 R 安装所有尚未安装的 CRAN 软件包?

The following R commands will install all CRAN packages:

availablePackages <- available.packages()[,1]
install.packages(availablePackages)

And the following command will list all installed packages:

installedPackages <- .packages(all.available = TRUE)

My question is: How do I instruct R to install all CRAN packages that are not already installed?

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

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

发布评论

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

评论(6

╄→承喏 2024-08-26 08:53:38

坦率地说,我认为这是一项艰苦的工作......它会持续几天,甚至几周(取决于资源),但这是代码(我只是喜欢做琐碎的事情):

# get names of installed packages
packs <- installed.packages()
exc <- names(packs[,'Package'])

# get available package names
av <- names(available.packages()[,1])

# create loooong string
ins <- av[!av %in% exc]
install.packages(ins)

我仍然不明白你为什么这样做,但是,嘿...有些事情就是不应该的...
最让我惊讶的是你已经回答了你自己的问题!你已经得到了你需要的东西,接下来就取决于你如何将它们组合在一起......
我们是否错过了重点?你还有别的想法吗?!?

Frankly, I think it's painstaking job... it would last for days, even weeks (depending on resources), but here's the code (I just enjoy doing trivial things):

# get names of installed packages
packs <- installed.packages()
exc <- names(packs[,'Package'])

# get available package names
av <- names(available.packages()[,1])

# create loooong string
ins <- av[!av %in% exc]
install.packages(ins)

I still don't get why you're doing this, but, hey... some things are just not meant to be....
What wonders me the most is the fact that you've already answered your own question! You got what you needed, and it's just up to you to put things together...
Are we missing the point? Did you have something else in mind?!?

彻夜缠绵 2024-08-26 08:53:38

1)你为什么要这么做?其中有超过 3500(截至 2012 年 2 月)?

2)您是否查看了 CRAN 任务视图ctv 包允许您从给定任务安装包?

3)您的粗体问题是一个简单的索引查询,您可以手动执行(除此之外,还请参阅help(sets)

R> available <- LETTERS                  # a simple set
R> installed <- LETTERS[c(1:10, 15:26)]  # a simple subset
R> available[ ! available %in% installed ]
[1] "K" "L" "M" "N"
R> 

编辑:响应您的关注-up:

a) 如果软件包未通过 Linux 和 Windows 上的“R CMD 检查”,则不会将其上传到 CRAN。那么这项工作就完成了。

b) 正如你将看到的,让所有依赖都归于你自己也是一项工作。我们为 cran2deb 做了这个,它位于 http://debian.cran.r-project.org (它进行成熟的 Debian 软件包构建,而不仅仅是安装)。我们构建了 2150 个软件包中的大约 2050 个。有一些我们因为许可证而拒绝构建,有一些我们因为缺少标头或库而无法构建,还有一些我们无法构建,因为它们需要例如 BioConductor 包。

1) Why would you want to do that? There are over 3500 (as of Feb 2012) of them?

2) Did you look at CRAN Task Views and the ctv package that allows you to install packages from a given task?

3) You bold-face question is a simple indexing query you can do by hand (and besides that, also see help(sets))

R> available <- LETTERS                  # a simple set
R> installed <- LETTERS[c(1:10, 15:26)]  # a simple subset
R> available[ ! available %in% installed ]
[1] "K" "L" "M" "N"
R> 

Edit: in response to your follow-up:

a) If a package does not pass 'R CMD check' on Linux and Windows, it does not get uploaded to CRAN. So that job is done.

b) Getting all depends at your end is work too as you will see. We did it for cran2deb which is at http://debian.cran.r-project.org (which does full-blown Debian package building which is more than just installing). We get about 2050 out of 2150 packages built. There are a few we refuse to build because of license, a few we cannot because of missing headers or libs and a few we cannot build because they need e.g. BioConductor packages.

快乐很简单 2024-08-26 08:53:38

输入此命令,然后所有软件包将自动安装:

install.packages(available.packages()[,1])

type this command and then all packages will be installed automatically:

install.packages(available.packages()[,1])
〆一缕阳光ご 2024-08-26 08:53:38

如果您使用更好:

    check.and.install.Package<-function(package_name){
        if(!package_name%in%installed.packages()){
           install.packages(package_name)
        }
     }

调用该函数并检查是否安装了所需的包:

    check.and.install.Package("pkgName")

Better if you use:

    check.and.install.Package<-function(package_name){
        if(!package_name%in%installed.packages()){
           install.packages(package_name)
        }
     }

call the function and check if required package is installed:

    check.and.install.Package("pkgName")
花辞树 2024-08-26 08:53:38

我已经测试过这个并且它

availablePackages=available.packages()
availablePackages<-as.vector(availablePackages[,1])
installedPackages=.packages(all.available = TRUE)
missedPackages<-setdiff(availablePackages, installedPackages)
for (i in 1:length(missedPackages))
{
pkgName <- missedPackages[i]
install.packages(pkgName)
}
print("END")

有效

I've tested this and it works

availablePackages=available.packages()
availablePackages<-as.vector(availablePackages[,1])
installedPackages=.packages(all.available = TRUE)
missedPackages<-setdiff(availablePackages, installedPackages)
for (i in 1:length(missedPackages))
{
pkgName <- missedPackages[i]
install.packages(pkgName)
}
print("END")

Regards

月朦胧 2024-08-26 08:53:38

根据我的经验,一次性安装所有 R 软件包并不明智!即使您不调用(使用库函数)所有这些包,只要放在主目录中,它们也会减慢您的 R 工作室的速度。至少我的例子就是这样。

From my experience, it is not wise to install all the R packages at once! Even if you do not call upon (using library function) all those packages, by just sitting in the home directory, they can slow down your R studio. At least that's what happened in my case.

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