data-raw/population19_msoa11.R

# ---- Load ----
library(tidyverse)
library(devtools)
library(readxl)
library(httr)

# Load package
load_all(".")

# Set query url
query_url <-
  query_urls |>
  filter(id == "estimates19_msoa11") |>
  pull(query)

GET(query_url,
    write_disk(tf <- tempfile(fileext = ".zip")))

# Where to store the unzipped files temporarily
td <- tempdir()
unzip_dir <- file.path(td, "population-msoa")

# Delete the temp folder if it already exists
if (dir.exists(unzip_dir))
  unlink(unzip_dir, recursive = TRUE)

dir.create(unzip_dir)

unzip(tf, exdir = unzip_dir)

msoa_pop <- read_excel(file.path(unzip_dir, "SAPE22DT4-mid-2019-msoa-syoa-estimates-unformatted.xlsx"),
                       sheet = "Mid-2019 Persons", 
                       skip = 4)

# Select and rename vars
msoa_pop <-
  msoa_pop |>
  select(
    msoa11_name = `MSOA Name`,
    msoa11_code = `MSOA Code`,
    `total_population` = `All Ages`,
    `0`:`90+`
  ) |>
  distinct()

# Rename
population19_msoa11 <- msoa_pop

# Save output to data/ folder
usethis::use_data(population19_msoa11, overwrite = TRUE)

unlink(tf); rm(tf)
unlink(td); rm(td)
humaniverse/demographr documentation built on June 10, 2025, 5:39 p.m.