Last active
February 10, 2018 10:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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