R/functions.R

Defines functions get_election_results

Documented in get_election_results

#' Get elections data for Kosovo
#'
#' @param type c("national", "local", "mayor", "mayor2")
#' @param year From 2010 and above
#' @param by c("party", "candidate")
#' @param level c("national", "municipality", "pc", "ps")
#'
#' @return
#' @export
#'
#' @examples
#'
#' data <- get_election_results(type = "national", year = 2021, by = "candidate", level = "municipality")
get_election_results <- function(type = c("national", "local", "mayor", "mayor2"),
                                 year = NULL,
                                 by = c("party", "candidate"),
                                 level = c("national", "municipality", "pc", "ps")){
  if (missing(type)){
    stop("'type = ' must have one of the following elements: 'national', 'local', 'mayor', 'mayor2'.")
  }
  if (missing(year)){
    stop("'year = ' must have one of the following elements: '2021', '2019', '2017', '2014', '2013'.")
  }
  if (missing(by)){
    stop("'by = ' must have one of the following elements: 'party', 'candidate'.")
  }
  if (missing(level)){
    stop("'level = ' must have one of the following elements: 'national', 'municipality', 'pc', 'ps'.")
  }
  linku <- stringr::str_glue("https://github.com/Kushtrimvisoka/datasets/blob/main/elections/{type}/{year}/{by}/{level}.RData?raw=true")
  rlang::inform(stringr::str_glue("Downloading data for {year} {type}, by {by}, at {level} level..."))
  load(url(linku))
  return(data)
}
Kushtrimvisoka/kosovoelections documentation built on Jan. 28, 2022, 4:06 a.m.