Below is a minimal template for tables meant to be presented in Statbank Greenland.
creation_date <- "20240301 09:00"
last_updated <- "20240711 09:00"
next_update <- "20250301 09:00"
year <- "2024"
subject <-
tribble(~language, ~value,
"en", "Population",
"da", "Befolkning",
"kl", "Innuttaasut")
description <-
subject %>%
mutate(value = str_glue("{value} 1999-{year} <em>[BEEP24MORT]</em>"))
x <-
population_gl %>%
pxmake::px() %>%
px_codepage("utf-8") %>%
px_language("en") %>%
px_languages(c("en", "da", "kl")) %>%
px_decimals("15") %>%
px_showdecimals("0") %>%
px_matrix("POP") %>%
px_subject_code("BE") %>%
px_subject_area(subject) %>%
px_contents(subject) %>%
px_stub(c("gender", "age")) %>%
px_heading("year") %>%
px_timeval("year") %>%
px_description(description) %>%
px_title(subject) %>%
px_units(tribble(~language, ~value,
"en", "Persons",
"da", "Personer",
"kl", "Inuit")) %>%
px_domain(tribble(~`variable-code`, ~language, ~domain,
"age" , "en", "VPAge",
"age" , "da", "VPAlder",
"age" , "kl", "VPUkiut")) %>%
px_update_frequency("Annually") %>%
px_creation_date(creation_date) %>%
px_last_updated(last_updated) %>%
px_next_update(next_update) %>%
px_link("www.stat.gl/bee202401/m1") %>%
px_contact("Lars Pedersen, LARP at STAT.gl") %>%
px_source(tribble(~language, ~value,
"en", "Statistics Greenland",
"da", "Grønlands Statistik",
"kl", "Kalaallit Nunaanni Naatsorsueqqissaartarfik"))
px files are published in Statbank Greenland and organized into subjects/topics. The api (pxweb) exposes this information for each supported language in https://bank.stat.gl/api/v1/{language}/Greenland.
ChatGPT can be used to generate R code. In this example, api endpoints adresses to subject (codes and texts) are provided and ChatGPT i asked to:
Please combine data from
https://bank.stat.gl/api/v1/en/Greenland,
https://bank.stat.gl/api/v1/da/Greenland and
https://bank.stat.gl/api/v1/kl/Greenland
into one dataframe in r and add column with language, in tidyverse,
fetch as function
This is returned:
# by ChatGPT
# Load the necessary packages
library(httr)
library(jsonlite)
library(tidyverse)
# Function to fetch data from API and add language column
fetch_data <- function(url, language) {
response <- GET(url)
data <- content(response, as = "text")
json_data <- fromJSON(data, flatten = TRUE) %>% as_tibble()
json_data <- json_data %>% mutate(language = language)
return(json_data)
}
# URLs for the APIs
url_en <- "https://bank.stat.gl/api/v1/en/Greenland"
url_da <- "https://bank.stat.gl/api/v1/da/Greenland"
url_kl <- "https://bank.stat.gl/api/v1/kl/Greenland"
# Fetch data from each API
df_en <- fetch_data(url_en, 'English')
df_da <- fetch_data(url_da, 'Danish')
df_kl <- fetch_data(url_kl, 'Greenlandic')
# Combine the dataframes
combined_df <- bind_rows(df_en, df_da, df_kl)
# Clean and inspect the combined dataframe
# View the first few rows of the combined dataframe
head(combined_df)
# Check the structure of the combined dataframe
glimpse(combined_df)