absmapsdata icon indicating copy to clipboard operation
absmapsdata copied to clipboard

fix problematic naming of correspondence tables

Open RWParsons opened this issue 1 year ago • 0 comments

The correspondence tables that map to 2021 edition of ASGS are mis-named, leading to problems with strayr::read_correspondence_tbl().

For example, the object with the correspondence table for SA2- 2016 to SA2-2021 is named CG_SA2_2016_SA2_2021.csv meaning that

read_correspondence_tbl("sa2",2016,"sa2",2021) fails read_correspondence_tbl("sa2",2016,"sa2","2021.csv") works

I have been getting around this with my own version of strayr's read_correspondence_tbl():

read_correspondence_tbl <- function(from_area,
                                    from_year,
                                    to_area,
                                    to_year,
                                    export_dir = tempdir()) {
  if (!dir.exists(export_dir)) {
    stop("export_dir provided does not exist: ", export_dir)
  }
  url <- "https://github.com/wfmackey/absmapsdata/raw/master/R/sysdata.rda"
  out_path <- file.path(export_dir, "cg_tables.rda")
  if (!file.exists(out_path)) {
    tryCatch(download.file(url, destfile = out_path, mode = "wb"),
      error = "Download failed. Check that you have access to the internet and that your requested object is available at https://github.com/wfmackey/absmapsdata/tree/master/data"
    )
  } else {
    message("Reading file found in ", export_dir)
  }
  load(out_path)

  e <- rlang::current_env()
  objs <- ls(envir = e)

  for (.name in objs) {
    x <- get(.name, envir = e)
    if (stringr::str_detect(.name, ".csv")) {
      newName <- stringr::str_remove(.name, ".csv")
      assign(newName, x, envir = e)
      rm(list = .name, envir = e)
    }
  }

  filename <- paste("CG", toupper(from_area), from_year, toupper(to_area),
    to_year,
    sep = "_"
  )
  cg_tbl <- try(get(filename))
  if (inherits(cg_tbl, "try-error")) {
    message("The following correspondence tables are available:")
    for (obj in ls()[str_detect(ls(), "^CG_")]) {
      message(obj)
    }
    stop("Correspondence table not available.")
  }
  cg_tbl
}

but feel that the fix should be made when these objects are made and written to sysdata.rda here.

RWParsons avatar Oct 03 '24 04:10 RWParsons