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.