简体   繁体   English

优雅地提取CRAN上未列出的包的R包依赖关系

[英]elegantly extract R package dependencies of a package not listed on CRAN

Given the DESCRIPTION file of an R package, I would like to obtain a list of the package dependencies (eg Depends , Imports , Suggests ). 给定R包的DESCRIPTION文件,我想获得包依赖的列表(例如DependsImportsSuggests )。 It seems like this should be a solved problem (eg by devtools::install_github ) but I cannot seem to come up with an elegant way to do this. 看起来这应该是一个解决的问题(例如通过devtools::install_github ),但我似乎无法想出一个优雅的方法来做到这一点。 My current solution is to rely on an unexported function from tools : 我目前的解决方案是依靠tools未导出的功能:

## adapted.from biocLite("pkgDepTools")
cleanPkgField <- function(val) {
    if (is.na(val))
      return(character(0))
    val <- names(tools:::.split_dependencies(val))
    if (is.null(val))
      return(character(0))
    val <- val[! val %in% "R"]
    if (length(val))
      return(val)
    return(character(0))
}

get_deps <- function(dp){
  dcf <- read.dcf(paste0(dp, "/DESCRIPTION"))
  suggests <- imports <- depends <- NULL
  if("Suggests" %in% colnames(dcf))
  suggests <- cleanPkgField(dcf[,"Suggests"])
  if("Imports" %in% colnames(dcf))
  imports <- cleanPkgField(dcf[,"Imports"])
  if("Depends" %in% colnames(dcf))
  depends <- cleanPkgField(dcf[,"Depends"])

  c(suggests, imports, depends)

  ## Doesn't work for packages not on CRAN
#  unlist(tools::package_dependencies(package_names, available.packages(),
#         which=c("Depends", "Imports", "Suggests"), recursive=FALSE))
}

where dp is the package directory. 其中dp是包目录。 This seems to work, but it feels like there should be an extant function to do this, or at least something cleaner than relying on the hidden and non-exported .split_dependencies() function in the tools package. 这似乎有效,但感觉应该有一个现存的功能来做到这一点,或者至少比依赖tools包中隐藏和未导出的.split_dependencies()函数更清晰。

Note that the more widely cited way to get dependencies for a package is not to rely on the DESCRIPTION file at all, but rather to use something like tools::package_dependencies which assumes the package can be found on some CRAN-like repository, eg these SO questions: 请注意,更广泛引用的获取包的依赖关系的方法根本不依赖于DESCRIPTION文件,而是使用类似tools::package_dependencies东西,它假设包可以在某个类似CRAN的存储库中找到,例如这些所以问题:

note that while the problem description is the same, the fact that the package is not on CRAN (or similar repository) makes that approach impossible. 请注意,虽然问题描述相同,但包不在CRAN(或类似的存储库)上的事实使得该方法不可能。

This is at least a bit simpler and doesn't rely on any unexported functions: 这至少有点简单,不依赖于任何未导出的函数:

get_deps <- function(path) {
    dcf <- read.dcf(file.path(path, "DESCRIPTION"))
    jj <- intersect(c("Depends", "Imports", "Suggests"), colnames(dcf))
    val <- unlist(strsplit(dcf[, jj], ","), use.names=FALSE)
    val <- gsub("\\s.*", "", trimws(val))
    val[val != "R"]
}

## Test it out on a source package with no imports ...
get_deps("C:/R/Source/Library/raster")
##  [1] "methods"   "sp"        "rgdal"     "rgeos"     "ncdf"      "ncdf4"    
##  [7] "igraph"    "snow"      "tcltk"     "rasterVis"

## ... and an installed package with no dependencies at all
get_deps("C:/R/Library/RColorBrewer/")
# named character(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM