Last active
March 8, 2021 09:26
-
-
Save jeffeaton/6489741f6cc232738bdb59ebc118d79e to your computer and use it in GitHub Desktop.
Adjust PEPFAR Data Pack export for 2021 specification
This file contains hidden or 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
#' This function saves an updated data pack export CSV in the same directory as | |
#' the Naomi output package file | |
#' | |
#' Example: | |
#' | |
#' file <- "~/Downloads/MWI_20210308-090536_naomi_spectrum_digest.zip" ## !! REPLACE HERE | |
#' adjust_datapack_export2021(file) | |
#' | |
adjust_datapack_export2021 <- function(naomi_output_package) { | |
tmpd <- tempfile() | |
on.exit(unlink(tmpd, recursive = TRUE, force = TRUE)) | |
unzip(naomi_output_package, file = "pepfar_datapack_indicators_2021.csv", exdir = tmpd) | |
dp <- readr::read_csv(file.path(tmpd, "pepfar_datapack_indicators_2021.csv")) | |
#' Add dataelement_uid column | |
datapack_indicators <- readr::read_csv("https://raw.githubusercontent.com/mrc-ide/naomi/master/inst/datapack/datapack_indicator_mapping.csv") | |
dp <- dplyr::left_join(dp, | |
dplyr::select(datapack_indicators, | |
indicator_code = datapack_indicator_code, | |
dataelement_uid = datapack_indicator_id), | |
by = "indicator_code" | |
) | |
dp <- dplyr::mutate(dp, | |
dataelement_uid = dplyr::if_else(indicator_code == "TX_CURR_SUBNAT.R", | |
"MktYDp33kd6", dataelement_uid) | |
) | |
dp <- dplyr::select(dp, psnu, psnu_uid, area_id, indicator_code, dataelement_uid, everything()) | |
#' Change age column to =""<01"" | |
dp$age <- paste0("=\"", dp$age, "\"") | |
outfile <- sub("naomi-output", "datapack-export", naomi_output_package) | |
outfile <- sub("zip$", "csv", outfile) | |
readr::write_csv(dp, outfile, na = "") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment