Skip to content

Instantly share code, notes, and snippets.

@cecilialee
Last active February 10, 2018 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cecilialee/ec8da128f39678e92f7e1347501a7524 to your computer and use it in GitHub Desktop.
Save cecilialee/ec8da128f39678e92f7e1347501a7524 to your computer and use it in GitHub Desktop.
Get full content of a single gist using GitHub's API in R. #r
# Get a single gist
library(httr)
url <- "https://api.github.com"
user <- "cecilialee"
r <- GET(sprintf("%s/users/%s/gists", url, user))
gists <- content(r)
raw_url <- gists[[1]]$files[[1]]$raw_url
r <- GET(raw_url)
script <- content(r)
cat(script)
# Get all gists
## Only getting the 1st file in each gist
library(httr)
library(tidyverse)
library(plyr)
url <- "https://api.github.com"
user <- "cecilialee"
r <- GET(sprintf("%s/users/%s/gists", url, user))
gists <- content(r)
get_gists <- function(gist) {
filename <- gist$files[[1]]$filename
raw_url <- gist$files[[1]]$raw_url
list(filename = filename, raw_url = raw_url)
}
get_content <- function(raw_url) {
r <- GET(as.character(raw_url))
content(r)
}
all_gists <- ldply(lapply(gists, get_gists), data.frame)
scripts <- sapply(all_gists$raw_url, get_content)
all_gists$script <- scripts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment