Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download entire repository from Github using R?

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 :)

like image 334
Inkling Avatar asked Feb 04 '18 20:02

Inkling


People also ask

How do I download a repository from GitHub to R?

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.

How do I download a whole GitHub 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 .

Can I download a whole folder from GitHub?

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.


3 Answers

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 #
like image 183
Cristian E. Nuno Avatar answered Oct 26 '22 05:10

Cristian E. Nuno


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

like image 21
jsta Avatar answered Oct 26 '22 04:10

jsta


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.

like image 2
gbganalyst Avatar answered Oct 26 '22 04:10

gbganalyst