如何从 CRAN 中提取所有包作者的姓名

How can I extract the names of all package authors from CRAN

为了庆祝 标签中的第 100,000 个问题,我想在 CRAN 上创建一个包含所有包作者姓名的列表。

最初,我认为我可以使用 available.packages() 来做到这一点,但遗憾的是这不包含作者专栏。

pdb <- available.packages()
colnames(pdb)

 [1] "Package"               "Version"               "Priority"             
 [4] "Depends"               "Imports"               "LinkingTo"            
 [7] "Suggests"              "Enhances"              "License"              
[10] "License_is_FOSS"       "License_restricts_use" "OS_type"              
[13] "Archs"                 "MD5sum"                "NeedsCompilation"     
[16] "File"                  "Repository"   

此信息可在每个包的 DESCRIPTION 文件中找到。于是想到了两种暴力破解方式,都不是很优雅:

  1. 下载每个 6,878 包并使用 base::read.dcf()

  2. 读取 DESCRIPTION 文件
  3. 抓取 CRAN 上的每个包页面。例如,https://cran.r-project.org/web/packages/MASS/index.html 告诉我 Brian Ripley 是 MASS 的作者。

我不想下载所有 CRAN 来回答这个问题。而且我也不想抓取 HTML,因为 DESCRIPTION 文件中的信息是 person 对象的格式整齐的列表(参见 ?person)。

如何使用 CRAN 上的信息轻松构建软件包作者列表?

为什么不将 Gabor 的 API 用于 CRAN 包?

例如http://crandb.r-pkg.org/MASS

library("httr")
content(GET("http://crandb.r-pkg.org/MASS"))$Author
[1] "Brian Ripley [aut, cre, cph],\nBill Venables [ctb],\nDouglas M. Bates [ctb],\nKurt Hornik [trl] (partial port ca 1998),\nAlbrecht Gebhardt [trl] (partial port ca 1998),\nDavid Firth [ctb]"

摘自 reverse_dependencies_with_maintainers,曾在 R developer site 上可用(我现在在那里看不到):

  description <- sprintf("%s/web/packages/packages.rds",
                          getOption("repos")["CRAN"])
  con <- if(substring(description, 1L, 7L) == "file://") {
       file(description, "rb")
  } else {
      url(description, "rb")
  }
  db <- as.data.frame(readRDS(gzcon(con)),stringsAsFactors=FALSE)
  close(con)
  rownames(db) <- NULL

  head(db$Author)
  head(db$"Authors@R")

Authors@R 存在的地方,可以使用 dget()

将其解析为更好的东西
getAuthor <- function(x){
  if(is.na(x)) return(NA)
  a <- textConnection(x)
  on.exit(close(a))
  dget(a)
}
authors <- lapply(db$"Authors@R", getAuthor)
head(authors)

[[1]]
[1] NA

[[2]]
[1] "Gaurav Sood <gsood07@gmail.com> [aut, cre]"

[[3]]
[1] "Csillery Katalin <kati.csillery@gmail.com> [aut]"
[2] "Lemaire Louisiane [aut]"                         
[3] "Francois Olivier [aut]"                          
[4] "Blum Michael <michael.blum@imag.fr> [aut, cre]"  

[[4]]
[1] NA

[[5]]
[1] "Csillery Katalin <kati.csillery@gmail.com> [aut]"
[2] "Lemaire Louisiane [aut]"                         
[3] "Francois Olivier [aut]"                          
[4] "Blum Michael <michael.blum@imag.fr> [aut, cre]"  

[[6]]
[1] NA