Created
January 16, 2024 04:44
-
-
Save paulovillarroel/a4240f21420ebb37f6bd38a6665e2c09 to your computer and use it in GitHub Desktop.
Function to use the data.gob.cl API
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
library(httr) | |
library(jsonlite) | |
library(dplyr) | |
url <- "https://datos.gob.cl/api/3/action/datastore_search?resource_id=2c44d782-3365-44e3-aefb-2c8b8363a1bc" | |
# Option A: paginated download | |
download_paginated_data <- function(url) { | |
all_data <- list() | |
offset <- 0 | |
repeat { | |
current_url <- modify_url(url, query = list(offset = offset)) | |
response <- GET(current_url) | |
data <- content(response, "text") |> fromJSON() | |
if (length(data$result$records) == 0) break | |
all_data <- c(all_data, list(data$result$records)) | |
offset <- offset + 100 | |
} | |
result_df <- do.call(bind_rows, all_data) | |
return(result_df) | |
} | |
all_data <- download_paginated_data(url) | |
# Option B: download using limit | |
response <- GET(url) | |
data <- content(response, "text") |> fromJSON() | |
limit <- data$result$total | |
url_limit <- modify_url(url, query = list(limit = limit)) | |
response_limit <- GET(url_limit) | |
all_data <- content(response_limit, "text") |> fromJSON() | |
all_data <- all_data$result$records |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment