Last active
February 19, 2018 08:26
-
-
Save cecilialee/031b3eb3ae7bd1c39b2af283fa118b1e to your computer and use it in GitHub Desktop.
Get all Airtable tables with API in R. #r #jsonlite #httr
This file contains hidden or 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
| library(tidyverse) | |
| library(httr) | |
| library(jsonlite) | |
| # API set up | |
| api <- "https://api.airtable.com/v0/apphVdH23b9rXFd1p/" | |
| api_key <- "keyL1Ja4L6pg3Uu9Q" # fake api_key | |
| tables <- c("products", "brands", "stores") | |
| # GET request | |
| urls <- paste0(api, tables) | |
| resps <- lapply(urls, GET, query = list("api_key" = api_key)) | |
| # Parse JSON objects | |
| contents <- list() | |
| for (i in seq_along(resps)) { contents[[i]] <- content(resps[[i]], as = "text") } | |
| jsons <- lapply(contents, fromJSON) | |
| # Assign dataframes | |
| df <- lapply(jsons, as.data.frame) | |
| for (i in seq_along(df)) assign(tables[[i]], df[[i]]) | |
| # Unnest dataframes | |
| unnest_dataframes <- function(x) { | |
| y <- do.call(data.frame, x) | |
| if ("data.frame" %in% sapply(y, class)) unnest_dataframes(y) | |
| y | |
| } | |
| for (i in seq_along(df)) assign(tables[[i]], unnest_dataframes(mget(tables)[[i]])) | |
| # Wrangle columns names | |
| wrangle_names <- function(df) { | |
| names(df) %>% | |
| str_replace_all("records.", "") %>% | |
| str_replace_all("fields.", "") %>% | |
| tolower() | |
| } | |
| all_tables <- lapply(tables, get) | |
| for (i in seq_along(tables)) { names(all_tables[[i]]) <- wrangle_names(all_tables[[i]]) } | |
| for (i in seq_along(df)) assign(tables[[i]], all_tables[[i]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment