R/utilValidateElectionYears.R

utilValidateElectionYears <- function(year, sinceYear){

  # find out current year
  currYear <- as.integer(format.Date(Sys.Date(), format = "%Y"))

  # vector of all election years which have available data for download
  electionYears <- seq(from = 2002, to = currYear, by = 2)

  # if no year was passed to the "year" parameter
  if (is.na(year)) {
    # assume we'll download data from all elections available
    year <- electionYears
  }

  if (electionType == "LOCAL") {
    validYears <- seq(from = 2004, to = currYear, by = 4)
  } else{
    if (electionType == "MAJOR"){
      validYears <- seq(from = 2002, to = currYear, by = 4)
    } else{
      validYears <- electionYears
    }
  }

  # we should only try extract data from valid years
  year <- intersect(year, validYears)

  # if no year was left, display error message
  if (length(year) < 1) {
    stop("Please type valid election year(s)")
  }

  # if the "sinceYear" parameter got a value
  if (!is.na(sinceYear)){
    # fill a vector of all valid years since "sinceYear"
    year <- intersect(validYears[validYears > min(sinceYear)])
  }

  return(year)
}
brunomssmelo/TseWrangler documentation built on May 13, 2019, 8:07 a.m.