是否可以使用 R 从 Github 下载整个存储库?我想要访问的这些文件不是 .csv 文件(大多数教程都会教授)。它们是 .r 和 .rmd 文件的混合,我想单独或一次性将其读入 R。 谢谢:)
最佳答案
概述
您可以使用 R 通过三个步骤从 GitHub 下载整个存储库:
- 从 GitHub repository of interest 上的
克隆或下载
按钮复制 .zip URL。请务必复制Download ZIP
中的链接地址,而不是 HTTPS URL。
注意:此步骤假设您对感兴趣的 GitHub 存储库的 main
分支感兴趣。如果不是这种情况,则为 be sure to navigate to the branch you are interested in downloading 。请注意,main
现在是 GitHub 存储库主分支的默认名称。如需更多上下文,请输入 read this article by Alexis Moody hosted on DEV 和 this article by GitHub 。
将 .zip URL 粘贴到
download.file()
的url
参数中以下载感兴趣的 .zip 文件。由于这是一个 GitHub 存储库,因此为destfile
参数分配与感兴趣的存储库相同的名称会很有帮助(在本例中为destfile = "meetingsR-master"
) 。destfile
参数名称的“-master”部分来自声明您希望下载的感兴趣存储库的分支名称。- 注意:请参阅第一步中的注释,了解为什么您可能需要将“master”替换为术语“main”。
使用
unzip()
解压下载的 .zip 文件。
可重现的示例
使用下面的代码时请注意更改文件路径。
# set working directory so I know where the .zip file will be located
setwd(dir = "/some/path/")
# download a .zip file of the repository
# from the "Clone or download - Download ZIP" button
# on the GitHub repository of interest
download.file(url = "https://github.com/jumpingrivers/meetingsR/archive/master.zip"
, destfile = "meetingsR-master.zip")
# unzip the .zip file
unzip(zipfile = "meetingsR-master.zip")
# set the working directory
# to be inside the newly unzipped
# GitHub repository of interest
setwd(dir = "/some/path/meetingsR-master/")
# examine the contents
list.files()
# [1] "_book"
# [2] "_output.yml"
# [3] "01-events.Rmd"
# [4] "02_useR_groups_aaa.Rmd"
# [5] "02_useR_groups_asia.Rmd"
# [6] "02_useR_groups_europe.Rmd"
# [7] "02_useR_groups_middle_east_africa.Rmd"
# [8] "02_useR_groups_north_america.Rmd"
# [9] "02_useR_groups_oceania.Rmd"
# [10] "02_useR_groups_south_america.Rmd"
# [11] "03-Rladies.Rmd"
# [12] "css"
# [13] "deploy.sh"
# [14] "DESCRIPTION"
# [15] "docs"
# [16] "index.Rmd"
# [17] "inverse.png"
# [18] "logo.png"
# [19] "Makefile"
# [20] "NAMESPACE"
# [21] "R"
# [22] "README.md"
# [23] "Rmeetings.Rproj"
# end of script #
关于r - 如何使用 R 从 Github 下载整个存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48612676/