R/constituencies.R

Defines functions constituencies

Documented in constituencies

#' House of Commons constituencies
#'
#' Imports data on House of Commons constituencies, returning a tibble of all
#' current and/or former Westminster constituencies, subject to parameters.
#'
#' @param current If `TRUE`, returns only current constituencies. If
#' `FALSE`, returns only former constituencies. If `NULL`, returns
#' all current and former constituencies. Defaults to `NULL`.
#' @inheritParams all_answered_questions
#' @return A tibble with details of Westminster constituencies.
#' @export
#' @examples
#' \dontrun{
#' x <- constituencies()
#'
#' y <- constituencies(current = FALSE)
#' }
#'
constituencies <- function(current = NULL, extra_args = NULL, tidy = TRUE,
                           tidy_style = "snake", verbose = TRUE) {
  baseurl <- paste0(url_util, "constituencies.json?")

  if (verbose == TRUE) {
    message("Connecting to API")
  }

  conts <- jsonlite::fromJSON(paste0(baseurl, extra_args), flatten = TRUE)

  jpage <- floor(conts$result$totalResults / 100)

  if (is.null(current)) {
    current_query <- ""
  } else if (current == TRUE) {
    current_query <- "&exists-endedDate=false"
  } else if (current == FALSE) {
    current_query <- "&exists-endedDate=true"
  } else {
    current_query <- ""
  }

  query <- paste0(baseurl, extra_args, current_query)

  df <- loop_query(query, jpage, verbose) # in utils-loop.R

  if (nrow(df) == 0) {
    message("The request did not return any data.
                Please check your parameters.")
  } else {
    if (tidy == TRUE) {
      df <- cons_tidy(df, current, tidy_style)
    }

    df
  }
}


#' @rdname constituencies
#' @export
hansard_constituencies <- constituencies

Try the hansard package in your browser

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

hansard documentation built on Nov. 13, 2019, 5:06 p.m.