列出R包依赖项而不安装包

zqdjd7g9  于 2023-07-31  发布在  其他
关注(0)|答案(6)|浏览(112)

有没有一种简单的方法可以获得一个给定包的R包依赖项列表(所有递归依赖项),而无需安装包及其依赖项?类似于portupgrade或apt中的假安装。

up9lanfz

up9lanfz1#

您可以使用available.packages函数的结果。例如,要查看ggplot2依赖于什么:

pack <- available.packages()
pack["ggplot2","Depends"]

字符串
其给出:

[1] "R (>= 2.14), stats, methods"


请注意,根据您想要实现的目标,您可能还需要检查Imports字段。

vaj7vani

vaj7vani2#

我很惊讶没有人提到tools::package_dependencies(),这是最简单的解决方案,并且有一个recursive参数(公认的解决方案没有提供)。
查看CRAN上前200个软件包的递归依赖关系的简单示例:

library(tidyverse)
avail_pks <- available.packages()
deps <- tools::package_dependencies(packages = avail_pks[1:200, "Package"],
                                    recursive = TRUE)

tibble(Package=names(deps),
       data=map(deps, as_tibble)) %>% 
  unnest(data)
#> # A tibble: 7,125 x 2
#>    Package value         
#>    <chr>   <chr>         
#>  1 A3      xtable        
#>  2 A3      pbapply       
#>  3 A3      parallel      
#>  4 A3      stats         
#>  5 A3      utils         
#>  6 aaSEA   DT            
#>  7 aaSEA   networkD3     
#>  8 aaSEA   shiny         
#>  9 aaSEA   shinydashboard
#> 10 aaSEA   magrittr      
#> # … with 7,115 more rows

字符串
创建于2020-12-04由reprex package(v0.3.0)

ulmd4ohb

ulmd4ohb3#

另一个简洁的解决方案是packrat库中的内部函数recursivePackageDependencies。但是,该软件包必须安装在您计算机上的 some 库中。其优点是它也适用于自制的非CRAN软件包。范例:

packrat:::recursivePackageDependencies("ggplot2", ignore = "", lib.loc = .libPaths()[1])

字符串
给出:

[1] "R6"           "RColorBrewer" "Rcpp"         "colorspace"   "dichromat"    "digest"       "gtable"      
 [8] "labeling"     "lazyeval"     "magrittr"     "munsell"      "plyr"         "reshape2"     "rlang"       
 [15] "scales"       "stringi"      "stringr"      "tibble"       "viridisLite"

lh80um4z

lh80um4z4#

我没有安装R,我需要找出哪些R包依赖于我公司要求使用的R包列表。
我写了一个bash脚本,它迭代文件中的R包列表,并将递归地发现依赖项。
该脚本使用名为rinput_orig.txt的文件作为输入(示例如下)。该脚本将创建一个名为rinput.txt的文件。
该脚本将创建以下文件:

*rdepsfound.txt-列出找到的依赖项,包括依赖于它的R包(下面的示例)。
*routput.txt-列出所有R软件包(来自原始列表和依赖项列表)沿着许可证和CRAN URL(示例如下)。
*r404.txt-尝试curl时收到404的R包列表。如果您的原始列表有任何错别字,这是很方便的。
Bash脚本:

#!/bin/bash

# CLEANUP
rm routput.txt
rm rdepsfound.txt
rm r404.txt

# COPY ORIGINAL INPUT TO WORKING INPUT
cp rinput_orig.txt rinput.txt

IFS=","
while read PACKAGE; do
    echo Processing $PACKAGE...

    PACKAGEURL="http://cran.r-project.org/web/packages/${PACKAGE}/index.html"

    if [ `curl -o /dev/null --silent --head --write-out '%{http_code}\n' ${PACKAGEURL}` != 404 ]; then
        # GET LICENSE INFO OF PACKAGE
        LICENSEINFO=$(curl ${PACKAGEURL} 2>/dev/null | grep -A1 "License:" | grep -v "License:" | gawk 'match($0, /<a href=".*">(.*)<\/a>/, a) {print a[0]}' | sed "s/|/,/g" | sed "s/+/,/g")
        for x in ${LICENSEINFO[*]}
        do
            # SAVE LICENSE
            LICENSE=$(echo ${x} | gawk 'match($0, /<a href=".*">(.*)<\/a>/, a) {print a[1]}')
            break
        done

        # WRITE PACKAGE AND LICENSE TO OUTPUT FILE
        echo $PACKAGE $LICENSE $PACKAGEURL >> routput.txt

        # GET DEPENDENCIES OF PACKAGE
        DEPS=$(curl ${PACKAGEURL} 2>/dev/null | grep -A1 "Depends:" | grep -v "Depends:" | gawk 'match($0, /<a href=".*">(.*)<\/a>/, a) {print a[0]}')
        for x in ${DEPS[*]}
        do
            FOUNDDEP=$(echo "${x}" | gawk 'match($0, /<a href=".*">(.*)<\/a>/, a) {print a[1]}' | sed "s/<\/span>//g")
            if [ "$FOUNDDEP" != "" ]; then
                echo Found dependency $FOUNDDEP for $PACKAGE...
                grep $FOUNDDEP rinput.txt > /dev/null
                if [ "$?" = "0" ]; then
                    echo $FOUNDDEP already exists in package list...
                else
                    echo Adding $FOUNDDEP to package list...
                    # SAVE FOUND DEPENDENCY BACK TO INPUT LIST
                    echo $FOUNDDEP >> rinput.txt
                    # SAVE FOUND DEPENDENCY TO DEPENDENCY LIST FOR EASY VIEWING OF ALL FOUND DEPENDENCIES
                    echo $FOUNDDEP is a dependency of $PACKAGE >> rdepsfound.txt
                fi
            fi
        done
    else
        echo Skipping $PACKAGE because 404 was received...
        echo $PACKAGE $PACKAGEURL >> r404.txt
    fi

done < rinput.txt
echo -e "\nRESULT:"
sort -u routput.txt

字符串

rinput_orig.txt示例:

shiny
rmarkdown
xtable
RODBC
RJDBC
XLConnect
openxlsx
xlsx
Rcpp

脚本运行时控制台输出示例:

Processing shiny...
Processing rmarkdown...
Processing xtable...
Processing RODBC...
Processing RJDBC...
Found dependency DBI for RJDBC...
Adding DBI to package list...
Found dependency rJava for RJDBC...
Adding rJava to package list...
Processing XLConnect...
Found dependency XLConnectJars for XLConnect...
Adding XLConnectJars to package list...
Processing openxlsx...
Processing xlsx...
Found dependency rJava for xlsx...
rJava already exists in package list...
Found dependency xlsxjars for xlsx...
Adding xlsxjars to package list...
Processing Rcpp...
Processing DBI...
Processing rJava...
Processing XLConnectJars...
Processing xlsxjars...
Found dependency rJava for xlsxjars...
rJava already exists in package list...

rdepsfound.txt示例:

DBI is a dependency of RJDBC
rJava is a dependency of RJDBC
XLConnectJars is a dependency of XLConnect
xlsxjars is a dependency of xlsx

routput.txt示例:

shiny GPL-3 http://cran.r-project.org/web/packages/shiny/index.html
rmarkdown GPL-3 http://cran.r-project.org/web/packages/rmarkdown/index.html
xtable GPL-2 http://cran.r-project.org/web/packages/xtable/index.html
RODBC GPL-2 http://cran.r-project.org/web/packages/RODBC/index.html
RJDBC GPL-2 http://cran.r-project.org/web/packages/RJDBC/index.html
XLConnect GPL-3 http://cran.r-project.org/web/packages/XLConnect/index.html
openxlsx GPL-3 http://cran.r-project.org/web/packages/openxlsx/index.html
xlsx GPL-3 http://cran.r-project.org/web/packages/xlsx/index.html
Rcpp GPL-2 http://cran.r-project.org/web/packages/Rcpp/index.html
DBI LGPL-2 http://cran.r-project.org/web/packages/DBI/index.html
rJava GPL-2 http://cran.r-project.org/web/packages/rJava/index.html
XLConnectJars GPL-3 http://cran.r-project.org/web/packages/XLConnectJars/index.html
xlsxjars GPL-3 http://cran.r-project.org/web/packages/xlsxjars/index.html

我希望这对某人有帮助!

tp5buhyn

tp5buhyn5#

我测试了我自己的解决方案(检查了本地安装的软件包)与packrattools的解决方案。
你可以发现不同方法之间的明显差异。
tools::package_dependencies看起来为旧的R版本(直到4.1.0和recursive = TRUE)给予太多,并且不是有效的解决方案。

R 4.1.0 NEWS
"Function tools::package_dependencies() (in package tools) can now use different dependency types for direct and recursive dependencies."

字符串
packrat:::recursivePackageDependencies使用的是available.packages,所以它是基于最新的远程包,而不是本地包。
默认情况下,我的函数是跳过基本包,如果你想附加它们,请更改base参数。
在R 4.1.0下测试:

get_deps <- function(package, fields = c("Depends", "Imports", "LinkingTo"), base = FALSE, lib.loc = NULL) {
  stopifnot((length(package) == 1) && is.character(package))
  stopifnot(all(fields %in% c("Depends", "Imports", "Suggests", "LinkingTo")))
  stopifnot(is.logical(base))
  stopifnot(package %in% rownames(utils::installed.packages(lib.loc = lib.loc)))

  paks_global <- NULL
  
  deps <- function(pak, fileds) {
    pks <- packageDescription(pak)
    res <- NULL
    for (f in fileds) {
      ff <- pks[[f]]
      if (!is.null(ff)) {
        res <- c(
          res,
          vapply(
            strsplit(trimws(strsplit(ff, ",")[[1]]), "[ \n\\(]"),
            function(x) x[1],
            character(1)
          )
        )
      }
    }
    if (is.null(res)) {
      return(NULL)
    }
    for (r in res) {
      if (r != "R" && !r %in% paks_global) {
        paks_global <<- c(r, paks_global)
        deps(r, fields)
      }
    }
  }
  
  deps(package, fields)
  
  setdiff(unique(paks_global), c(
     package,
     "R",
     if (!base) {
       c(
         "stats",
         "graphics",
         "grDevices",
         "utils",
         "datasets",
         "methods",
         "base", 
         "tools"
       )
     } else {
       NULL
     }
   ))
}

own = get_deps("shiny", fields = c("Depends", "Imports"))

packrat = packrat:::recursivePackageDependencies("shiny", lib.loc = .libPaths(), fields = c("Depends", "Imports"))

tools = tools::package_dependencies("shiny", which =  c("Depends", "Imports"), recursive = TRUE)[[1]]

setdiff(own, packrat)
#> character(0)
setdiff(packrat, own)
#> character(0)
setdiff(own, tools)
#> character(0)
setdiff(tools, own)
#> [1] "methods"   "utils"     "grDevices" "tools"     "stats"     "graphics"
setdiff(packrat, tools)
#> character(0)
setdiff(tools, packrat)
#> [1] "methods"   "utils"     "grDevices" "tools"     "stats"     "graphics"

own
#>  [1] "lifecycle"   "ellipsis"    "cachem"      "jquerylib"   "rappdirs"   
#>  [6] "fs"          "sass"        "bslib"       "glue"        "commonmark" 
#> [11] "withr"       "fastmap"     "crayon"      "sourcetools" "base64enc"  
#> [16] "htmltools"   "digest"      "xtable"      "jsonlite"    "mime"       
#> [21] "magrittr"    "rlang"       "later"       "promises"    "R6"         
#> [26] "Rcpp"        "httpuv"
packrat
#>  [1] "R6"          "Rcpp"        "base64enc"   "bslib"       "cachem"     
#>  [6] "commonmark"  "crayon"      "digest"      "ellipsis"    "fastmap"    
#> [11] "fs"          "glue"        "htmltools"   "httpuv"      "jquerylib"  
#> [16] "jsonlite"    "later"       "lifecycle"   "magrittr"    "mime"       
#> [21] "promises"    "rappdirs"    "rlang"       "sass"        "sourcetools"
#> [26] "withr"       "xtable"
tools
#>  [1] "methods"     "utils"       "grDevices"   "httpuv"      "mime"       
#>  [6] "jsonlite"    "xtable"      "digest"      "htmltools"   "R6"         
#> [11] "sourcetools" "later"       "promises"    "tools"       "crayon"     
#> [16] "rlang"       "fastmap"     "withr"       "commonmark"  "glue"       
#> [21] "bslib"       "cachem"      "ellipsis"    "lifecycle"   "sass"       
#> [26] "jquerylib"   "magrittr"    "base64enc"   "Rcpp"        "stats"      
#> [31] "graphics"    "fs"          "rappdirs"

microbenchmark::microbenchmark(get_deps("shiny", fields = c("Depends", "Imports")),
                               packrat:::recursivePackageDependencies("shiny", lib.loc = .libPaths(), fields = c("Depends", "Imports")),
                               tools = tools::package_dependencies("shiny", which =  c("Depends", "Imports"), recursive = TRUE)[[1]],
                               times = 5
)
#> Warning in microbenchmark::microbenchmark(get_deps("shiny", fields =
#> c("Depends", : less accurate nanosecond times to avoid potential integer
#> overflows
#> Unit: milliseconds
#>                                                                                                           expr
#>                                                            get_deps("shiny", fields = c("Depends", "Imports"))
#>  packrat:::recursivePackageDependencies("shiny", lib.loc = .libPaths(),      fields = c("Depends", "Imports"))
#>                                                                                                          tools
#>         min         lq       mean     median         uq        max neval
#>    5.316552   5.607365   6.054568   5.674359   6.633308   7.041258     5
#>   18.767340  19.387588  21.739127  21.581457  23.526169  25.433079     5
#>  411.589734 449.179354 458.526354 465.431262 468.440211 497.991207     5


创建于2021-06-25由reprex package(v0.3.0)
证明在旧的R版本下tools解决方案有问题。根据R 3.6.3进行测试。

paks <- tools::package_dependencies("shiny", which =  c("Depends", "Imports"), recursive = TRUE)[[1]]
"lifecycle" %in% paks
#> [1] TRUE
any(c(paks, "shiny") %in% tools::dependsOnPkgs("lifecycle"))
#> [1] FALSE


创建于2021-06-25由reprex package(v0.3.0)

1mrurvl1

1mrurvl16#

试试这个:tools::package_dependencies(recursive = TRUE)$package_name
举个例子-这里是dplyr的依赖项:

tools::package_dependencies(recursive = TRUE)$dplyr
 [1] "ellipsis"   "generics"   "glue"       "lifecycle"  "magrittr"   "methods"   
 [7] "R6"         "rlang"      "tibble"     "tidyselect" "utils"      "vctrs"     
[13] "cli"        "crayon"     "fansi"      "pillar"     "pkgconfig"  "purrr"     
[19] "digest"     "assertthat" "grDevices"  "utf8"       "tools"

字符串

相关问题