#' Fetches data snapshot from DataHub
#'
#' @examples
#' use with code from admin.datahub.is
#'
#' @export
fetch_snapshot <- function(sheet_name, snapshot_uuid) {
# Check input arguments
collection <- checkmate::makeAssertCollection()
checkmate::assertString(sheet_name, na.ok = FALSE, min.chars = 1, add = collection)
checkmate::reportAssertions(collection)
# Create url
url <- paste0(
getOption("base_path", default = "https://api.datahub.is"),
"/snapshot/download/",
snapshot_uuid,
"/",
sheet_name)
# Make API call and parse
resp <- httr::GET(
url,
httr::add_headers("Authorization" = getOption("authentication_key", default = "")),
httr::accept_json())
parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
#* Stop if errors
if (httr::http_error(resp)) {
stop(
sprintf(
"DataHub API request failed [%s]\n%s\n<%s>",
httr::status_code(resp),
parsed$message,
parsed$errors
),
call. = FALSE
)
}
tryCatch({
parsed <- jsonlite::fromJSON(parsed)
data <- as_tibble(parsed$data)
colnames(data) <- parsed$columns
print(parsed$columns)
data$date <- as.Date(data$date)
data <- mutate_if(data, is.character, as.numeric)
return(data)
},
error=function(error_message) {
return()
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.