如何确定R包的安装来源及对应GitHub仓库?
R包安装来源信息的查询方法
1. R是否存储包的安装来源,能否访问?
R会存储包的安装来源信息,这些内容记录在包的DESCRIPTION文件中,可通过packageDescription()函数直接读取:
# 查看目标包的完整描述信息 packageDescription("dplyr") # 提取特定字段 packageDescription("dplyr")$Repository packageDescription("dplyr")$RemoteType
2. 版本号相同时,区分CRAN与GitHub安装的包?
当包版本号一致时,可通过packageDescription()返回的字段差异区分来源:
- CRAN安装的包:包含
Repository: CRAN字段,无Remote开头的字段 - GitHub安装的包:存在
RemoteType: github字段,同时附带RemoteUsername(GitHub用户名)、RemoteRepo(仓库名)等字段
示例代码:
# 判断包是否来自CRAN is_cran <- function(pkg) { desc <- packageDescription(pkg) !is.null(desc$Repository) && desc$Repository == "CRAN" } # 判断包是否来自GitHub is_github <- function(pkg) { desc <- packageDescription(pkg) !is.null(desc$RemoteType) && desc$RemoteType == "github" } # 测试调用 is_cran("dplyr") is_github("dplyr")
3. 确定GitHub安装包的仓库或分支?
对于通过devtools或remotes从GitHub安装的包,packageDescription()会返回以下关键字段:
RemoteUsername:GitHub仓库所属的用户名/组织名RemoteRepo:仓库名称RemoteBranch:安装时使用的分支名
提取信息的代码示例:
get_github_info <- function(pkg) { desc <- packageDescription(pkg) if (desc$RemoteType != "github") { stop("该包并非从GitHub安装") } list( username = desc$RemoteUsername, repo = desc$RemoteRepo, branch = desc$RemoteBranch ) } # 示例:获取目标GitHub包的信息 get_github_info("ggplot2") # 需安装的是GitHub版本
内容的提问来源于stack exchange,提问作者mhovd
相关产品推荐
相关产品推荐

