Skip to content

Instantly share code, notes, and snippets.

@oganm
Last active October 16, 2018 19:45
Show Gist options
  • Save oganm/fb7dae5b1f043e70669b1b5a0529be75 to your computer and use it in GitHub Desktop.
Save oganm/fb7dae5b1f043e70669b1b5a0529be75 to your computer and use it in GitHub Desktop.
download single files from private or public github repositories
getGithubFile = function(githubPath,branch = 'master', downloadPath = NULL,token = NULL){
if(is.null(downloadPath)){
downloadPath = tempfile()
}
path = strsplit(githubPath,'/')[[1]]
file = paste(path[3:length(path)], collapse = '/')
contents = gh::gh('GET /repos/:username/:reponame/contents/:dir?ref=:branch',
username = path[1],
reponame = path[2],
branch = branch,
dir = dirname(file),
.token = token)
names(contents) = contents %>% purrr::map_chr('name')
fileInfo = contents[contents %>% purrr::map_chr('name') %>% {.%in%basename(file)}][[1]]
blob = gh::gh('GET /repos/:username/:reponame/git/blobs/:sha',
username = path[1],
reponame = path[2],
sha = fileInfo$sha,
.token = token)
decodeContent = openssl::base64_decode(blob$content)
writeBin(decodeContent,downloadPath)
return(invisible(downloadPath))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment