0

Apologies for the wall of text.

Based on this post and this post, primarily, I wrote a function to let me install a list of packages at the start of every R script, without having to go through the cycle of "Call function -> get error message -> install package" each time. The function is this:

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

However, when I try to run this, the output is a list of packages already installed. For example, this is my trial run:

```package_list <- c("dagitty","MMWRweek","ggplot2","parallel")```
```packages_installed(package_list)```

And this is my output:

```
[[1]]
 [1] "dagitty"       "parallel"      "CoxBoost"      "prodlim"      
 [5] "Matrix"        "survival"      "spatstat"      "rpart"        
 [9] "nlme"          "spatstat.data" "ggmap"         "leaflet"      
[13] "spdep"         "spData"        "sp"            "pdftools"     
[17] "data.table"    "forcats"       "stringr"       "dplyr"        
[21] "purrr"         "readr"         "tidyr"         "tibble"       
[25] "tidyverse"     "ggplot2"       "sf"            "RJSONIO"      
[29] "stats"         "graphics"      "grDevices"     "utils"        
[33] "datasets"      "methods"       "base"         

[[2]]
 [1] "MMWRweek"      "dagitty"       "parallel"      "CoxBoost"     
 [5] "prodlim"       "Matrix"        "survival"      "spatstat"     
 [9] "rpart"         "nlme"          "spatstat.data" "ggmap"        
[13] "leaflet"       "spdep"         "spData"        "sp"           
[17] "pdftools"      "data.table"    "forcats"       "stringr"      
[21] "dplyr"         "purrr"         "readr"         "tidyr"        
[25] "tibble"        "tidyverse"     "ggplot2"       "sf"           
[29] "RJSONIO"       "stats"         "graphics"      "grDevices"    
[33] "utils"         "datasets"      "methods"       "base"         

[[3]]
 [1] "MMWRweek"      "dagitty"       "parallel"      "CoxBoost"     
 [5] "prodlim"       "Matrix"        "survival"      "spatstat"     
 [9] "rpart"         "nlme"          "spatstat.data" "ggmap"        
[13] "leaflet"       "spdep"         "spData"        "sp"           
[17] "pdftools"      "data.table"    "forcats"       "stringr"      
[21] "dplyr"         "purrr"         "readr"         "tidyr"        
[25] "tibble"        "tidyverse"     "ggplot2"       "sf"           
[29] "RJSONIO"       "stats"         "graphics"      "grDevices"    
[33] "utils"         "datasets"      "methods"       "base"         

[[4]]
 [1] "MMWRweek"      "dagitty"       "parallel"      "CoxBoost"     
 [5] "prodlim"       "Matrix"        "survival"      "spatstat"     
 [9] "rpart"         "nlme"          "spatstat.data" "ggmap"        
[13] "leaflet"       "spdep"         "spData"        "sp"           
[17] "pdftools"      "data.table"    "forcats"       "stringr"      
[21] "dplyr"         "purrr"         "readr"         "tidyr"        
[25] "tibble"        "tidyverse"     "ggplot2"       "sf"           
[29] "RJSONIO"       "stats"         "graphics"      "grDevices"    
[33] "utils"         "datasets"      "methods"       "base"         

Warning messages:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘dagitty’
2: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘MMWRweek’
```

I understand the Warning Messages, which are output from the require call in the function. This is the output each time I call the function, even when the package is already installed and loaded. As far as I can tell, these are the packages in the global environment, and I'm unclear on how to prevent them from appearing. Any help is appreciated.

pogibas
  • 27,303
  • 19
  • 84
  • 117
KVemuri
  • 194
  • 1
  • 16

2 Answers2

0
necessary_packages <- c("DBI", "odbc")

new_packages <- necessary_packages[!(necessary_packages %in% installed.packages()[,"Package"])]

if(seq_along(new_packages) > 0){install.packages(new_packages, dependencies = TRUE)}

lapply(necessary_packages, require, character.only = TRUE)
hello_friend
  • 5,682
  • 1
  • 11
  • 15
  • Thanks a lot, I had a similar function to begin with as well. Then I got greedy and wanted to load the packages that were already installed, in the same step. The function above works perfectly well when I do not have the library step in it. What I wanted to know was why the output I showed above occurs. Not that it's wrong, just unexpected and a bit unnecessary. Let me know if you have any thoughts on that. – KVemuri Oct 12 '19 at 12:57
0

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

KVemuri
  • 194
  • 1
  • 16