Installing all CRAN packages that are not already installed?

knorv picture knorv · Jan 30, 2010 · Viewed 35.6k times · Source

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?

Answer

aL3xa picture aL3xa · Jan 31, 2010

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