Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant way to check for missing packages and install them?

Tags:

r

r-faq

packages

I seem to be sharing a lot of code with coauthors these days. Many of them are novice/intermediate R users and don't realize that they have to install packages they don't already have.

Is there an elegant way to call installed.packages(), compare that to the ones I am loading and install if missing?

like image 941
Maiasaura Avatar asked Nov 03 '10 18:11

Maiasaura


People also ask

How do you determine what packages are installed on a system?

You use the pkgchk command to check installation completeness, path name, file contents, and file attributes of a package. See pkgchk(1M) for more information on all the options. Use the pkginfo command to display information about the packages that are installed on the system.

How do you check if a package is installed or not in R?

Calling installed. packages() returns a detailed data frame about installed packages, not only containing names, but also licences, versions, dependencies and more.

What is the correct way to install the packages in R *?

Open R via your preferred method (icon on desktop, Start Menu, dock, etc.) Click “Packages” in the top menu then click “Install package(s)”. Choose a mirror that is closest to your geographical location. Now you get to choose which packages you want to install.

How do you install and load the packages in R?

In R, you can easily install and load additional packages provided by other users. or click Tools > Install packages. Write the package name in the dialog, then click install. Once you install the package, you need to load it so that it becomes available to use.


1 Answers

Yes. If you have your list of packages, compare it to the output from installed.packages()[,"Package"] and install the missing packages. Something like this:

list.of.packages <- c("ggplot2", "Rcpp") new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])] if(length(new.packages)) install.packages(new.packages) 

Otherwise:

If you put your code in a package and make them dependencies, then they will automatically be installed when you install your package.

like image 57
Shane Avatar answered Oct 18 '22 14:10

Shane