Elegant Way to Check For Missing Packages and Install Them

Elegant way to check for missing packages and install them?

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.

Elegant way to check for missing packages and install them?

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.

R check list of packages, then load or install

One way you could achieve this is by checking if it is installed using the installed.packages() function. Then, you can also check which packages are loaded by using the .packages() function.

When you implement this, it'd look like this:

# Declare packages
packages <- c("dplyr","ggplot2","magrittr")

# Loop through each package
for (package in packages) {

# Install package
# Note: `installed.packages()` returns a vector of all the installed packages
if (!package in installed.packages()) {

# Install it
install.packages(
package,
dependencies = TRUE
)

}

# Load package
# Note: `.packages()` returns a vector of all the loaded packages
if (!package in .packages()) {

# Load it
library(
package,
character.only = TRUE
)

}

}

Output of function to check for and install missing packages

A friend helped me find the answer in the documentation of the library function, which I should have read more closely before asking the question:

"Normally library returns (invisibly) the list of attached packages, but TRUE or FALSE if logical.return is TRUE. When called as library() it returns an object of class "libraryIQR", and for library(help=), one of class "packageInfo"."

Accordingly, I modified my function to only return whether it has been loaded or not.

packages_installed <- function(pkg_list){
pkgs <- unlist(pkg_list)
req <- unlist(lapply(pkgs, require, character.only = TRUE,
quietly = TRUE))
not_installed <- pkgs[req == FALSE]
lapply(not_installed, install.packages,
repos = "http://cran.r-project.org")#also add lib.loc later
sapply(pkgs, library, character.only = TRUE,
logical.return = TRUE, warn.conflicts = TRUE)

}

And now the output for the argument package_list

package_list <- list("rNOMADS","adehabitatMA","raster","rgdal","rgeos"); packages_installed(package_list)

reads as

rNOMADS adehabitatMA raster rgdal rgeos
TRUE TRUE TRUE TRUE TRUE

best way to install required packages in r

are you looking for something like this?

listOfPackages <- c("proto","gsubfn","tidyr","dplyr","ggplot2",
"stringr","magrittr","usmap","RCurl",
"RJSONIO","sqldf")
for (i in listOfPackages){
if(! i %in% installed.packages()){
install.packages(i, dependencies = TRUE)
}
require(i)
}

You can load a package with either library or require. The last one will not force the loading, if the package is already load, while the first one will.

Error when installing and loading libraries from list in R

I found that the following modified version, which is based on this answer and another post, will install all missing packages and load them without giving a warning:

# List of required packages
lib_List <- c("dplyr", "ggplot2", "scales", "caret")

# Install missing packages
instpack <- lib_List %in% installed.packages()[,"Package"]
if (any(instpack == FALSE)) {
install.packages(lib_List[!instpack])
}

# Load packages
invisible(lapply(lib_List, library, character.only = TRUE))

It's not the shortest solution, but the advantage is that no additional packages are required.

Elegant way to load a string list of packages in R

Here is one option

Pkgs <- c('caret','readxl')
lib <- .libPaths()[1]

i1 <- !(Pkgs %in% row.names(installed.packages()))
if(any(i1)) {
install.packages(Pkgs[i1], dependencies = TRUE, lib = lib)
}


Related Topics



Leave a reply



Submit