R/tts_list.R

Defines functions tts_list

Documented in tts_list

#' @title Import list of read-only Translation Tables as 
#' ipums-metadata Python objects
#' 
#' @description Imports a batch of PMA Translation Tables as a 
#' list object, where each item in the list contains a Python 
#' object returned by the ipums-metadata TranslationTable 
#' class (with all class methods attached as functions). 
#' 
#' This function is designed for use with tasks requiring fast 
#' queries delivered to a large number of Translation Tables 
#' (e.g. tagging Data Dictionaries with the function 
#' dd_tagging). If the user wishes to import just one 
#' Translation Table by name, use of this function is not 
#' recommended. Instead, they are recommended to use a 
#' function like \link{tt_get} or py$TranslationTable().
#' 
#' @param unit Optional character: "hhf", "sdp", "mnh", 
#' "nut_hhf", or "nut_sdp". The function will only import 
#'  Translation Tables found in unit-associated variables 
#'  folders (in addition to the common folder, and to 
#'  the person and sdp folders if "nut_hhf" or "nut_sdp"
#'  is selected). If no unit is specified, the function
#'  will list all PMA Translation Tables.
#' @param mod_since Optional: If a date is specified 
#' ("yyyy-mm-dd"), the function will only import Translation 
#' Tables modified after that date (vastly improving speed of
#' import).
#'   
#' @author Matt Gunther
#' 
#' @export tts_list
tts_list <- function(
  unit,
  mod_since
){
  here <- py$project_to_path("pma")
  if(missing(unit)){
    message("Finding all PMA Translation Tables...")
    TTpaths <- list.files(
      paste0(here, "/variables"),
      patt="tt.xls", 
      full.names = T,
      recursive = T
    )
    TTpaths <- grep(
      "archive|tt_work", 
      TTpaths, 
      val=T, 
      ign=T, 
      invert=T
    )
  } else {
    if(unit == "hhf" | unit == "nut_hhf"){
      TTpaths <- list.files(
        paste0(here, "/variables/person"),
        patt="tt.xls", 
        full.names = T)
      if(unit == "nut_hhf"){
        TTpaths <- append(
          TTpaths,
          list.files(
            paste0(here, "/variables/nutrition/person"),
            patt="tt.xls",
            full.names = T))
      }
    }else if(unit == "sdp" | unit == "nut_sdp"){
      TTpaths <- list.files(
        paste0(here, "/variables/service_provider"),
        patt="tt.xls", 
        full.names = T)
      if(unit == "nut_sdp"){
        TTpaths <- append(
          TTpaths,
          list.files(
            paste0(here, "/variables/nutrition/service_provider"),
            patt="tt.xls",
            full.names = T))
      }
    }else if(unit == "mnh"){
      TTpaths <- list.files(
        paste0(here, "/variables/mnh"),
        patt="tt.xls", 
        full.names = T)
    }
    TTpaths <- append(
      TTpaths, 
      list.files(
        paste0(here, "/variables/common"),
        patt="tt.xls", 
        full.names = T))
  }
  
  if(missing(mod_since)==F){
    message("Finding PMA Translation Tables modified since ",
            mod_since, " ...")
    mod_since <- as.Date(mod_since)
    mod_dates <- as.Date(file.info(TTpaths)$mtime)
    TTpaths <- TTpaths[which(mod_dates > mod_since)]
  }
  
  message("Importing list of Translation Tables: \n" )
  pb <- txtProgressBar(1, length(TTpaths), style=3)
  TTs <- c()
  for(i in 1:length(TTpaths)){
    TT <- TTpaths[i]
    TTs[[TT]] <- py$TranslationTable(TT,"pma")
    setTxtProgressBar(pb,i)
  }
  cat("\n")
  return(TTs)
}
mgunther87/ipumsPMA documentation built on Aug. 1, 2020, 12:22 a.m.