4

Hi I've written the following code:

################# Loadin Require Libraries #################
required.packages <- c('caret','readxl')
for (pkg in required.packages){
  if(!require(pkg, character.only = T)){
    install.packages(pkg,
                     character.only = T,
                     dependencies = T)
    library(pkg, character.only = T)
  }else{
    library(pkg, character.only = T)
  }

}

The code shall be ran on a computer of a peer, so to take care of might missing libraries I thought I iterate threw a string list, to check if the package is installed if yes -> load if no -> install and load then. However when a package is not available R still puts out a warning message: Warning message:

In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : es gibt kein Paket namens ‘readxl’

My question: is there a better way to check / install a bunch of libraries in R? Should I care about the warning? If it is not important is there a way to surpress this warning getting printed?

Edit: Final solution Thanks to the correct answer provided by @akrun:

################# Loadin Require Libraries #################
lib <- .libPaths()[1]
required.packages <- c('caret','readxl')
i1 <- !(required.packages %in% row.names(installed.packages()))
if(any(i1)) {
  install.packages(required.packages[i1], dependencies = TRUE, lib = lib) 
}
lapply(required.packages, require, character.only = TRUE)

Update 2021 - Pacman

I found the pacman - package really helpful for exactly this purpose, especially the p_load function. It checks if the package is installed and otherwise tries to install the missing package.

This function is a wrapper for library and require. It checks to see if a package is installed, if not it attempts to install the package from CRAN and/or any other repository in the pacman repository list.

So nowadays I start all my scripts that need to be 'portable' with the following lines:

require(pacman)  
# Load / Install Required Packages
p_load(dplyr, tidyr, gridExtra, psych)

In this case to load / install dplyr, tidyr, gridExtra & psych

Also nice in this package (if you want to clean up the environment) p_unload

# Unload All packages
p_unload()
SysRIP
  • 159
  • 8

1 Answers1

3

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) 
  }
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Will accept your answer asap. Ty for the fast response. Allow me a brief follow up question, in your snipped how would load the libs in Pkgs? I found this `lapply(x, require, character.only = TRUE)` as suggested by daroczig here https://stackoverflow.com/questions/8175912/load-multiple-packages-at-once – SysRIP Sep 02 '19 at 17:41
  • Thanks again, however when my peer (by an accident) already has one of these packages it still wont be loaded (at least for my test) since the if wont be ran. Would you then suggest the lapply solution? – SysRIP Sep 02 '19 at 17:51
  • @SysRIP Yes, the 'i1' checks if the package is already present. Not clear whehter you need to install it again or not – akrun Sep 02 '19 at 17:52
  • @SysRIP DId you meant that you to avoid installing one of the package? – akrun Sep 02 '19 at 17:59
  • 1
    I am now good with the final solution. I wanted to provied my colleague with a scirpt which installs the packages he doesn't already have and then load all packages required. So that if the script gets runned a second time not all packages are unnecessarily installed again. Thanks a lot for the fast and fruitful help – SysRIP Sep 02 '19 at 18:03
  • it wont install again as wehave the checks – akrun Sep 02 '19 at 18:04