首页 > 解决方案 > 如何从 github 下载(不安装)包

问题描述

无论如何从github下载压缩包而不安装它?

例如运行:

devtools::install_github("tidyverse/tidyr")

一次下载和安装。有什么相当于

download.packages("tidyr", destdir = "path")

对于 github 包?

标签: rdevtools

解决方案


如果您想下载 GitHub 存储库(在本例中为tidyr包),您可以使用download.file并通过右键单击复制 GitHub“克隆或下载”按钮中的链接。

download.file(url = "https://github.com/tidyverse/tidyr/archive/master.zip",
              destfile = "tidyr.zip")

如果你想要一个函数来做到这一点,一个可能的解决方案可能是(它将下载到当前工作目录):

download_git <- function(repo_name, repo_url, install = FALSE){

   url_git <- paste0(file.path(repo, "archive", "master"), ".zip")
   download.file(url = url_git,
                 destfile = paste0(repo_name, "-master.zip"))

   if(install) {

      unzip(zipfile = paste0(repo_name, "-master.zip"))

      devtools::install(paste0(repo_name,"-master"))    
   }
}

你这里是一个如何使用它的例子(带有安装选项):

download_git(repo_name = "tidyr", 
             repo_url = "https://github.com/tidyverse/tidyr", 
             install = TRUE)

推荐阅读