R/download-overnightyields.R

#' Download overnight yields from Bank of Canada website.
#'
#' @param years_span              A positive integer

#' @return overnightyields_bofc   Data.frame of overnight yields for the last \code{years_span} years
#'
#' @examples
#' # For the last year of data
#' download_overnight(1)
#'
#' # For the last 10 years of data
#' download_overnight(10)
#'
download_overnight <- function( years_span = 10 ){
  # Get url query string to grab data
  query_str <- create_overnight_query_string( years_span = years_span )

  # Download dataset
  temp <- tempfile( )
  download.file( query_str, temp, mode = "wb", quiet = TRUE )

  # Read in dataset and clean up
  overnightyields_bofc <- read.csv( temp, header = FALSE, stringsAsFactors = FALSE, skip = 12 )
  # create column names
  new_colnames <- c( "dates", "OVERNIGHT" )
  names( overnightyields_bofc ) <- new_colnames

  # Update dataset and return it
  unlink( temp )
  return( overnightyields_bofc )
}
vathymut/bondyields documentation built on May 3, 2019, 4:35 p.m.