R/functions.R

Defines functions clean_dir make_animation compile_latex get_age_profile

# Hello, world!
#
# This is an example function named 'hello'
# which prints 'Hello, world!'.
#
# You can learn more about package authoring with RStudio at:
#
#   http://r-pkgs.had.co.nz/
#
# Some useful keyboard shortcuts for package authoring:
#
#   Build and Reload Package:  'Ctrl + Shift + B'
#   Check Package:             'Ctrl + Shift + E'
#   Test Package:              'Ctrl + Shift + T'
#' @export
get_age_profile = function(dat, column_name, fun = lapply, categories = c(), num = 0) {
  start_time = Sys.time()
  descriptive_moments = fun(seq(1996, 2100), function(year) {
    new_data = dat[Year == year]
    cat(paste('\rComputing year: ', year))
    return(rbindlist(lapply(seq(18, 79), function(age) {
      new_new_data = new_data[Age == age]
      category_perc = c()
      category_tot = c()
      category_all = c()
      for (c in categories) {
        category_perc = c(category_perc, mean(new_new_data[[column_name]] == c))
        category_tot = c(category_tot, sum(new_new_data[[column_name]] == c))
        category_all = c(category_all, length(new_data[[column_name]]))
      }
      return(data.table(
        age = age,
        name = factor(c("mean",
                        paste(rep('cat_perc', length(categories)), categories, sep = '_'),
                        paste(rep('cat_sum', length(categories)), categories, sep = '_'),
                        paste(rep('cat_all', length(categories)), categories, sep = '_'))),
        val = c(mean(new_new_data[[column_name]]),
                category_perc,
                category_tot,
                category_all),
        year = year,
        num = num,
        total_in_year = nrow(new_data)
      ))
    })))
  })
  cat(paste('\rTime elapsed:', Sys.time() - start_time, 'min'))
  return(rbindlist(descriptive_moments))
}

#' @export
compile_latex = function(dir) {
  curr_dir = getwd()
  setwd(dir)
  files = list.files('.')
  for (file in files) {
    if (strsplit(file, "\\.")[[1]][2] == "tex") {
      system(paste("pdflatex", file))
    }
  }
  setwd(curr_dir)
}

#' @export
make_animation = function(dir) {
  curr_dir = getwd()
  setwd(dir)
  #for (file in list.files('.')) {
  #  if (strsplit(file, "\\.")[[1]][2] == "pdf") {
  #    system(paste('"C:\\Program Files\\ImageMagick-7.0.6-Q16\\convert.exe" -density 300 -background white -alpha remove ', file, ' ', strsplit(file, "\\.")[[1]][1], '.png', sep = ''))
  #  }
  #}
  system('"C:\\Program Files\\ImageMagick-7.0.6-Q16\\convert.exe" -delay 5 -density 300 -background white -alpha remove *.pdf animation.gif')
  system('"C:\\Program Files\\ImageMagick-7.0.6-Q16\\convert.exe" -delay 20 -density 300 -background white -alpha remove *.pdf animation_slow.gif')
  setwd(curr_dir)
}

#' @export
clean_dir = function(dir) {
  curr_dir = getwd()
  setwd(dir)
  for (file in list.files('.')) {
    if (strsplit(file, "\\.")[[1]][2] != "pdf" & strsplit(file, "\\.")[[1]][2] != "gif") {
      file.remove(file)
    }
  }
  setwd(curr_dir)
}
penn-wharton-budget-model/plotsim documentation built on Aug. 19, 2017, 12:17 a.m.