Is it possible to download an entire repository from Github using R? These files I want to access are NOT .csv files (which most tutorials teach). They are a mix of .r and .rmd files, which I want to read into R either separately or all at once. Thanks :)
The most direct way to get data from Github to your computer/ into R, is to download the repository. That is, click the big green button: The big, green button saying “Clone or download”, click it and choose “download zip”. Of course, for those using Git and Github, it would be appropriate to clone the repository.
When downloading materials to your laptop, it is easiest to download the entire repository. To do this, go to the GitHub page for the workshop, click on the green Code button, then download the repository as a ZIP file. The filename may be different than what's in the picture. Find the downloaded .
GitHub User InterfaceThere's a download button on the repository's homepage. Of course, this downloads the entire repo, after which you would need to unzip the download and then manually drag out the specific folder you need.
You can download an entire repository from GitHub using R in three steps:
Clone or download
button on the GitHub repository of interest. Be sure to copy the link address from Download ZIP
and not the HTTPS URL.Note: This step assumes you are interested in the main
branch of the GitHub repository of interest. If this is not the case, be sure to navigate to the branch you are interested in downloading. Please note that main
is now the default name of the main branch of a GitHub repository. For more context, please read this article by Alexis Moody hosted on DEV and this article by GitHub.
Paste the .zip URL into the url
parameter of download.file()
to download the .zip file of interest. Since this is a GitHub repository, it is helpful to assign the destfile
parameter the same name as the repository of interest (in this case, destfile = "meetingsR-master"
). The "-master" portion of the destfile
parameter name comes from declaring the branch name of the repository of interest that you wish to download.
Use unzip()
to unzip the downloaded .zip file.
Be mindful to change the file paths when using the code down below.
# 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 #
I see this question has the rstudio
tag. You can use rstudio
(and avoid the command-line) by selecting file -> new project -> version control -> git and entering the address of your desired Github repository in the Repository URL
field.
After you hit the Create Project
button, rstudio
will download the contents of the repository, create a new project, and change your working directory to the new project.
See http://happygitwithr.com/rstudio-git-github.html#clone-the-new-github-repository-to-your-computer-via-rstudio
You can download an entire repository from GitHub using R by installing the package usethis
:
install.packages('usethis')
Copy the .git URL from the Clone or download button on the GitHub repository of interest. Be sure to copy the link address from Download ZIP and not the HTTPS URL.
For example, I want to download this repository. I will copy the link address from Download ZIP (https://github.com/cwickham/purrr-tutorial.git) and paste it in usethis::use_course()
and then remove the .git and replace it with /archive/master.zip
usethis::use_course('https://github.com/cwickham/purrr-tutorial/archive/master.zip')
You then follow the prompt question from R about where to save the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With