R/download_rvapp_cut.R

Defines functions download_rvapp_cut

Documented in download_rvapp_cut

#' Download RVAPP data cut from ProcXed
#'
#' Downloads the most recent RVAPP data cut from
#' s0260a\\dbxed.
#'
#' Can only download the most recent data cut.
#'
#' @param year Financial year, YYYY-YY. Character.
#' @param quarter Quarter, either as Q1 or just 1
#' @param uid User ID passed to \code{\link{connect_to_procxed}}
#'
#' @return A tibble
#' @export
#'
#' @examples
#' \dontrun{
#' download_rvapp_cut(year = "2021-22", quarter = "Q2", uid = "U123456")
#' }
download_rvapp_cut <- function(fin_year,
                               quarter,
                               uid) {


  # Sort out inputs -----------------------------------------------------------
  ## Extract just the number if quarter is entered as "Q3"
  quarter <- quarter %>%
    stringr::str_match("[0-9]") %>%
    as.numeric()

  ## Extract just the start of the calendar year
  fin_year <- fin_year %>%
    stringr::str_sub(1, 4)

  ## Combined into YYYYQ for the table name in the server
  rvapp_table <- paste0("rvappeals_", fin_year, quarter)

  # Download data from server -------------------------------------------------
  ## Open connection to ProcXed server
  procxed_con <- ndr::connect_to_procxed(uid, .in_func = TRUE)

  ## Download RVAPP data
  data <- dplyr::tbl(procxed_con,
                     dbplyr::in_schema("rvapp", rvapp_table)) %>%
    tibble::as_tibble()

  ## Close connection to ProcXed server
  DBI::dbDisconnect(procxed_con)

  ## Return the downloaded data
  return(data)
}
n-fanton/ndr documentation built on Dec. 21, 2021, 11:07 p.m.