How to download entire repository from Github using R?

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.


Overview

You can download an entire repository from GitHub using R in three steps:

  1. Copy the .zip 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.

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.

enter image description here

  1. 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.

    • Note: please see note in step one to understand why you might need to replace "master" with the term "main".
  2. Use unzip() to unzip the downloaded .zip file.

Reproducible Example

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 #