11

I am wondering if there exists a function or a link or any method that would work like list.files() function in R but on a folder stored on a github repostiory.

example github repository folder: https://github.com/KZPS/Spotkania/tree/master/Matteo/literature

Thanks for any advice !

Marcin
  • 7,834
  • 8
  • 52
  • 99
  • 1
    You could always execute a system call, e.g. `system('git ls-tree --full-tree -r HEAD')` . See https://stackoverflow.com/questions/8533202/list-files-in-local-git-repo for possibly useful info – Carl Witthoft Aug 25 '14 at 12:12

2 Answers2

21

Here's one way:

library(httr)
req <- GET("https://api.github.com/repos/KZPS/Spotkania/git/trees/master?recursive=1")
stop_for_status(req)
filelist <- unlist(lapply(content(req)$tree, "[", "path"), use.names = F)
grep("Matteo/literature/", filelist, value = TRUE, fixed = TRUE)
# [1] "Matteo/literature/Subsetting.pdf"     
# [2] "Matteo/literature/datatable-intro.pdf"

You could easily build a function list.files.github from that.

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • 1
    To fetch just one folder you can also use the following URL: https://api.github.com/repos/KZPS/Spotkania/contents/Matteo/literature – Amedeo Baragiola Apr 05 '15 at 21:03
  • using `r-lib/gh` that wrap the api.github.com you can use `GET /repos/:owner/:repo/git/trees/:tree_sha?recursive=1` see https://developer.github.com/v3/git/trees/#get-a-tree-recursively – aurelien Oct 19 '18 at 11:53
  • Just in case, to build the URL: https://api.github.com/repos/[USER]/[REPO]/git/trees/[BRANCH]?recursive=1 FROM: https://stackoverflow.com/a/61656698/1873521 – Gorka Jun 27 '22 at 14:12
-1

I forked the repo then cloned the fork on my local directory. I made the forked the directory my working directory and was able to use list.files()

  • 1
    This means you download repository to know what files are in it. But I wanted only to know what files are on github to download only specific files. Not a good idea of you – Marcin Jan 23 '15 at 20:22
  • 1
    @MarcinKosinski Your assessment is fair enough. I'm sure there are much better ways. – Aaron Hardisty Jan 28 '15 at 17:47