R/auselect_winners.R

Defines functions auselect_winners

Documented in auselect_winners

#' Retrieve a .csv dataset from the australian_elections GitHub repository.
#'
#' @description
#' `auselect_winners()` downloads and assigns a filtered dataset based upon the election winners to a given variable.
#'
#' @param w_ids A boolean value used to determine if the dataset is returned with or without a unique identifier column.
#' Default is set to TRUE, which returns a dataset with the unique identifier column.
#'
#'
#' @details
#' The `auselect_winners()` function is used to return a pre-filtered version of either the `voting_data_with_ids` or `voting_data` datasets.
#' On call, the function will filter the requested dataset with only election winners (`dummyWinners == 1`).
#' The default dataset that is downloaded through this function is the `voting_data_with_ids` dataset.
#' Setting the argument value of the function to `w_ids = F` will set the function to instead download the `voting_data` dataset.
#'
#' @return A filtered dataset with either unique identifier values or no unique identifier values as specified by \code{w_ids}.
#'
#' @examples
#'
#' \dontrun{
#' # Return the filtered dataset with unique ids.
#' winners_ids <- auselect_winners()
#'
#' # Preview dataset
#' head(winners_ids)
#'
#' # Request the filtered dataset without unique ids.
#' winners_noids <- auselect_winners(w_ids = F)
#'
#' # Preview the dataset.
#' head(winners_noids)
#' }
#'
#' @export
# Function to access a filtered set of Australian elections datasets.
# Takes one arguments `w_ids`.
# A boolean value to determine whether to download dataset with unique ids or without.


auselect_winners <- function(w_ids = T){
  dwnlds <- c("https://raw.github.com/RohanAlexander/australian_federal_elections/master/outputs/voting_data.csv",
              "https://raw.github.com/RohanAlexander/australian_federal_elections/master/outputs/voting_data_with_ids.csv"

  )

  tmpdir <- tempfile(fileext = ".csv")

  if(w_ids == T){
    utils::download.file(dwnlds[[2]], tmpdir, quiet = F)

    x <- readr::read_csv(tmpdir, show_col_types = F)

    dplyr::filter(x, winnerDummy == 1)
  }

  else if(w_ids == F){
    utils::download.file(dwnlds[[1]], tmpdir, quiet = F)

    x <- readr::read_csv(tmpdir, show_col_types = F)

    dplyr::filter(x, winnerDummy == 1)
  }
}
RohanAlexander/AustralianElections documentation built on Dec. 18, 2021, 10:59 a.m.