18

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?

knorv
  • 49,059
  • 74
  • 210
  • 294
  • 2
    Another simple use case is setting up a laptop to another analyst, who may be working offline. Rather than trying to anticipate what packages they will use, just install them all. My department takes this approach when configuring analysts laptops. – fmark May 25 '12 at 02:15
  • 1
    `setdiff(letters, letters[5:15])` – baptiste Mar 16 '14 at 11:38

6 Answers6

20

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?!?

aL3xa
  • 35,415
  • 18
  • 79
  • 112
  • 3
    "I still don't get why you're doing this" If you're in a high security environment where you have no internet access. Mirroring CRAN is what you do. Source: I have to do this. – mythicalprogrammer Jun 18 '20 at 05:50
  • I know this is an old thread, but this is very useful. I get an opportunity once or twice a year to open the firewall and pull down everything. I have a window of about 48 hours.Then take my time to patch all the environments to the same level. I really don't want to mess around trying to stand up a mirror when I have plenty of bandwidth and storage. – Colin Nov 03 '20 at 16:28
  • 1
    How much disk space is needed to install all CRAN packages? – Robert Hume Feb 15 '22 at 11:41
  • @mythicalprogrammer have you succeeded on installing all CRAN packages? Just curious how long did it take. I'm a HPC user, and probably doing this by the same reason of you. – Ricardo Barros Lourenço May 18 '23 at 18:19
13

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • I want to make sure my system has all the right dependencies, and I'm testing that by trying to build the entire CRAN :-) Does CRAN contain many broken packages that I should expect won't build? – knorv Jan 30 '10 at 20:03
  • There are a few that you should expect won't build if you don't have libraries installed in standard places. rgdal for example assumes GDAL is installed somewhere it's configure script can find. If you have it installed in a non-standard location, you will have to manually point R CMD INSTALL to the right place. – Sharpie Feb 01 '10 at 04:27
  • @Dirk "Over two thousand" remains a valid estimate, but I, umm, took the liberty of reducing the residual. – Iterator Feb 08 '12 at 13:57
  • @Iterator: Please do not edit my posts. It was clearly timestamped and the number /was clearly relevant at the time it was given/. If you are that troubled by the value and you must edit please at least time stamp your edit and sign it. – Dirk Eddelbuettel Feb 08 '12 at 16:25
  • 1
    @DirkEddelbuettel Apologies. Though edits to update seem to be within site intentions, that's also why I dropped a note, to let you know - in case it mattered. As for timestamps - these are all part of the edit facility & logging (it's all version controlled)... – Iterator Feb 08 '12 at 16:49
2

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

install.packages(available.packages()[,1])
loki
  • 9,816
  • 7
  • 56
  • 82
  • 3
    how does this improve on any of the previous answers?? – Ben Bolker Mar 16 '14 at 03:19
  • 1
    The downside of this approach is that your session might encounter a problem at some point and end. Using aL3xa's approach means that each time you run the code it has fewer packages to install than the last time you tried to run it. – Andrew Brēza Dec 17 '16 at 20:14
1

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")
shilovk
  • 11,718
  • 17
  • 75
  • 74
0

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

0

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.