R/download_bin.R

Defines functions download_bin

Documented in download_bin

#' @title Downloads the elections zip file
#'
#' @param url The URL of the elections zip to download
#' @param tempfile The path to save the downloaded file
#'
#' @return NULL
#'
#' @importFrom httr GET
#' @importFrom httr add_headers
#' @importFrom httr stop_for_status
#' @importFrom httr timeout
#'
#' @keywords internal
#'
download_bin <- function(url, tempfile) {
  UA <- paste(
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0)",
    "Gecko/20100101 Firefox/98.0"
  )

  if (file.exists(tempfile)) {
    message(
      "File already exists, skipping download. Reading existing file ",
      gsub("..+/", "", tempfile)
    )
  } else {
    message("Downloading ", url)
    res <- GET(
      url,
      add_headers(`User-Agent` = UA, Connection = "keep-alive"),
      timeout(30)
    )
    stop_for_status(res)
    writeBin(res$content, tempfile)
  }
}

Try the infoelectoral package in your browser

Any scripts or data that you put into this service are public.

infoelectoral documentation built on April 29, 2026, 9:07 a.m.