如何在我的R库中找出从GitHub安装的软件包?

时间:2018-04-06 17:42:15

标签: r github devtools install.packages

我想知道当前库中有多少个软件包是从GitHub安装的,但是找不到办法来实现它

# The number of installed packages in my library
length(.packages(all.available=TRUE))
[1] 145

这个R-bloggers帖子显示了软件包的版本,但没有显示它们的安装位置 https://www.r-bloggers.com/list-of-user-installed-r-packages-and-their-versions/

ip <- as.data.frame(installed.packages()[, c(1, 3:4)])
rownames(ip) <- NULL
ip <- ip[is.na(ip$Priority), 1:2, drop=FALSE]
print(ip, row.names=FALSE)

              Package     Version
                abind       1.4-5
              acepack       1.4.1
                 ade4      1.7-10
            albersusa       0.3.0
        AnnotationDbi      1.40.0
          ansistrings       1.0.0
                  ape         5.0
                  aqp        1.15
                  ash      1.0-15
           assertthat       0.2.0
                astsa         1.8
                ATmet         1.2
              automap      1.0-14
            backports       1.1.2
               base64         2.0
            base64enc       0.1-3
                bazar       1.0.6
               BBmisc        1.11
             beeswarm       0.2.3
                   BH    1.66.0-1

我以为我可以加载所有包然后运行devtools::session_info()来找到我想要的东西 https://www.r-bloggers.com/loading-all-installed-r-packages/

lapply(.packages(all.available=TRUE), 
        function(x) library(x, character.only=TRUE))

然后我遇到了另一个问题:同时加载太多包maximal number of DLLs reached...。套餐changepoint仅是100多个套餐中的第53个套餐

 Error: package or namespace load failed for ‘changepoint’ in inDL(x, as.logical(local), as.logical(now), ...):
 unable to load shared object 'C:/RCat/library/changepoint/libs/x64/changepoint.dll':
  `maximal number of DLLs reached... 

编辑1 :我使用了@Dason建议的代码,但遇到了这些错误

# empty folder
> sapply(dir(.libPaths()), isGithub)
Error: $ operator is invalid for atomic vectors
In addition: Warning message:
In packageDescription(pkg) :
  DESCRIPTION file of package 'file31043e741b3f' is missing or broken

# only lattice.dll left in lattice/lib/x64  
> sapply(dir(.libPaths()), isGithub)
Error: $ operator is invalid for atomic vectors
In addition: Warning message:
In packageDescription(pkg) :
  DESCRIPTION file of package 'lattice' is missing or broken

非常感谢您的帮助!!!

3 个答案:

答案 0 :(得分:7)

使用来源。如果您检查devtools::session_info()的代码,则相关信息似乎位于devtools::package_info()。 package_info的代码是:

> getAnywhere("package_info")
A single object matching ‘package_info’ was found
It was found in the following places
  namespace:devtools
with value

function (pkgs = loadedNamespaces(), include_base = FALSE, libpath = NULL) 
{
    desc <- suppressWarnings(lapply(pkgs, packageDescription, 
        lib.loc = libpath))
    not_installed <- vapply(desc, identical, logical(1), NA)
    if (any(not_installed)) {
        stop("`pkgs` ", paste0("'", pkgs[not_installed], "'", 
            collapse = ", "), " are not installed", call. = FALSE)
    }
    if (!include_base) {
        base <- vapply(pkgs, pkg_is_base, logical(1))
        pkgs <- pkgs[!base]
    }
    pkgs <- sort_ci(pkgs)
    attached <- pkgs %in% sub("^package:", "", search())
    desc <- lapply(pkgs, packageDescription, lib.loc = libpath)
    version <- vapply(desc, function(x) x$Version, character(1))
    date <- vapply(desc, pkg_date, character(1))
    source <- vapply(desc, pkg_source, character(1))
    pkgs_df <- data.frame(package = pkgs, `*` = ifelse(attached, 
        "*", ""), version = version, date = date, source = source, 
        stringsAsFactors = FALSE, check.names = FALSE)
    rownames(pkgs_df) <- NULL
    class(pkgs_df) <- c("packages_info", "data.frame")
    pkgs_df
}
<bytecode: 0x000000000e211f50>
<environment: namespace:devtools>

基本上,utils :: packageDescription()的输出传递给devtools :: pkg_source()。因此,如果您需要,您可以检查packageDescription的输出是什么样的,并编写一个函数来识别描述是否将其标记为github包。虽然我没有进行过广泛的测试,但我第一次通过它。

isGithub <- function(pkg){!is.null(packageDescription(pkg)$GithubRepo)}

然后在我们所有的软件包上运行它,我们就可以在.libPaths中列出文件夹

sapply(dir(.libPaths()), isGithub)

答案 1 :(得分:1)

感谢@Dason,我终于有了这个工作

查找从GitHub安装的软件包的功能

isGithub <- function(pkg){
    !is.null(packageDescription(pkg)$GithubRepo)
}

获取本地库中的所有包

my_lib <- as.data.frame(library()$result, stringsAsFactors=FALSE)

检查哪些软件包来自GitHub

result <- sapply(my_lib$Package, isGithub)
head(df[df$result == TRUE, ])

     names.result. result
4           bindr   TRUE
5        bindrcpp   TRUE
6        blogdown   TRUE
9          chroma   TRUE
17          dplyr   TRUE
21          editR   TRUE

答案 2 :(得分:0)

进行一些小的修改,使您可以浏览库并确定每个库的来源,包括github存储库的名称

library(tidyverse)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang
#> Registered S3 method overwritten by 'rvest':
#>   method            from
#>   read_xml.response xml2
allmypackages <- as.data.frame(installed.packages())
allmypackages <- allmypackages %>%
  filter(Priority != "base" | is.na(Priority)) %>%
  select(-c(Enhances:MD5sum, LinkingTo:Suggests)) %>%
  droplevels()

package_source <- function(pkg){
  x <- as.character(packageDescription(pkg)$Repository)
  if (length(x)==0) {
    y <- as.character(packageDescription(pkg)$GithubRepo)
    z <- as.character(packageDescription(pkg)$GithubUsername)
    if (length(y)==0) {
      return("Other")
    } else {
      return(str_c("GitHub repo = ", z, "/", y))
    }
  } else {
    return(x)
  }
}

head(sapply(allmypackages$Package, package_source),60)
#>  [1] "CRAN"                            "CRAN"                           
#>  [3] "CRAN"                            "CRAN"                           
#>  [5] "CRAN"                            "CRAN"                           
#>  [7] "CRAN"                            "CRAN"                           
#>  [9] "CRAN"                            "CRAN"                           
#> [11] "CRAN"                            "CRAN"                           
#> [13] "CRAN"                            "CRAN"                           
#> [15] "CRAN"                            "CRAN"                           
#> [17] "CRAN"                            "CRAN"                           
#> [19] "CRAN"                            "CRAN"                           
#> [21] "CRAN"                            "CRAN"                           
#> [23] "CRAN"                            "CRAN"                           
#> [25] "CRAN"                            "CRAN"                           
#> [27] "CRAN"                            "CRAN"                           
#> [29] "CRAN"                            "CRAN"                           
#> [31] "CRAN"                            "CRAN"                           
#> [33] "CRAN"                            "CRAN"                           
#> [35] "CRAN"                            "CRAN"                           
#> [37] "CRAN"                            "CRAN"                           
#> [39] "CRAN"                            "CRAN"                           
#> [41] "CRAN"                            "CRAN"                           
#> [43] "CRAN"                            "CRAN"                           
#> [45] "Other"                           "R-Forge"                        
#> [47] "CRAN"                            "CRAN"                           
#> [49] "CRAN"                            "CRAN"                           
#> [51] "CRAN"                            "CRAN"                           
#> [53] "CRAN"                            "CRAN"                           
#> [55] "CRAN"                            "CRAN"                           
#> [57] "CRAN"                            "CRAN"                           
#> [59] "GitHub repo = cjtexas/colourgen" "CRAN"
allmypackages$whereat <- sapply(allmypackages$Package, package_source)

reprex package(v0.2.1)于2019-05-14创建