用于检查和安装缺少的软件包的功能输出

问题描述 投票:0回答:1

为文字墙道歉。

主要基于此post和此post,我编写了一个函数,使我可以在每个R脚本的开头安装软件包列表,而不必经历“调用函数-> get错误消息->安装软件包”。函数是这样的:

`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)
`

但是,当我尝试运行此程序时,输出是已经安装的软件包的列表。例如,这是我的试运行:

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

这是我的输出:

```
[[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’
```

我理解警告消息,该警告消息是从函数中的require调用输出的。每次调用该函数时,这都是输出,即使已经安装和加载了软件包也是如此。据我所知,这些是全球环境中的软件包,我不清楚如何防止它们出现。任何帮助表示赞赏。

r install.packages r-faq
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.