Created
April 4, 2022 14:40
-
-
Save long39ng/88351e56b1d51aa57b4a44a8db5f1655 to your computer and use it in GitHub Desktop.
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(httr2) | |
corona_platform_api <- function(endpoint, ...) { | |
request("https://www.corona-datenplattform.de/api/3/action") |> | |
req_url_path_append(endpoint) |> | |
req_url_query(...) |> | |
req_perform() |> | |
resp_body_json() | |
} | |
# package_show gibt nur die Metadaten aus (Token nicht nötig): | |
corona_platform_api("package_show", id = "bildungsniveau") |> | |
str(max.level = 2) | |
# Datensatz-ID | |
corona_resource_id <- function(dataset) { | |
corona_platform_api("package_show", id = dataset)$result$resources[[1]]$id | |
} | |
# Daten-Download ---------------------------------------------------------- | |
corona_data <- function(resource_id) { | |
resp <- request("https://www.corona-datenplattform.de/datastore/dump") |> | |
# Token vorher mit Sys.setenv() speichern | |
req_headers(Authorization = Sys.getenv("corona_datenplattform_token")) |> | |
req_url_path_append(resource_id) |> | |
req_url_query(format = "json") |> | |
req_perform() |> | |
resp_body_json(simplifyVector = TRUE) | |
dat <- as.data.frame(resp$records) | |
colnames(dat) <- resp$fields$id | |
purrr::map2_dfc(dat, resp$fields$type, as.type) | |
} | |
# util Funktion zur Konvertierung von Datentypen | |
as.type <- function(x, type, ...) { | |
switch(type, | |
int = as.integer(x, ...), | |
numeric = as.numeric(x, ...), | |
# else: | |
x | |
) | |
} | |
corona_data(resource_id = corona_resource_id("bildungsniveau")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment