R/getRfData.R

#' Get risk-free rate data.
#'
#' \code{getRfData} gets the risk-free rate data from the specified
#' directory and file. If the file does not exist, then it downloads the data
#' and stores it in the specified file.
#'
#' @export
#'
#' @param maturity Numerical, maturity of yield in years (integer 1 to 30). Has
#'   effect only if the file does not exist, or \code{force == TRUE}.
#' @param frequency Character, one of daily, weekly, monthly, quarterly,
#'   half-yearly, annual. Has effect only if the file does not exist,
#'   or \code{force == TRUE}.
#' @param file String, file name. Default is generated based on today's date.
#' @param dir String, directory of the file. Default is generated temporary
#'   directory.
#' @param start Date (or an object that can be parsed using \code{as.Date}),
#'   start of the period. Has effect only if the
#'   file does not exist, ot \code{force == TRUE}.
#' @param end Date (or an object that can be parsed using \code{as.Date}), end
#'   of the period. Has effect only if the
#'   file does not exist, ot \code{force == TRUE}.
#' @param force Logical, whether to force download data even if the file with
#'   the specified name already exists.
#' @param quite Logical, whether to suppress any output to console.
#'
#' @return String, file path where the downloaded data is stored.
#'
#' @source Data comes from
#'   \href{https://www.ecb.europa.eu/stats/money/yc/html/index.en.html}{ECB}.
#'
#' @seealso \code{\link{downloadRfData}}.
getRfData <- function(maturity = 1,
                      file = paste0("rf-data-", Sys.Date(), ".RData"),
                      dir = tempdir(),
                      start = "2004-09-06",
                      end = Sys.Date(),
                      force = FALSE,
                      quite = FALSE) {

  filePath <- paste(dir, file, sep = "/")

  if (!file.exists(filePath)){
    if (!quite) {
      cat("File not found. Downloading data into ", filePath, "\n")
    }
    downloadRfData(file = file,
                   dir = dir,
                   maturity = maturity,
                   start = start,
                   end = end)
    if (!quite) {
      cat("Data saved into", filePath, "\n")
    }
  } else if (force) {
    if (!quite) {
      cat("Force downloading data into.", filePath, "\n")
    }
    downloadRfData(file = file,
                   dir = dir,
                   maturity = maturity,
                   start = start,
                   end = end)
    if (!quite) {
      cat("Data saved into", filePath, "\n")
    }
  }

  if (!quite) {
    cat("File", filePath, "exists. Loading.\n")
  }
  load(filePath)
  if (!quite) {
    cat("Loaded.\n")
  }


  return(rfData)
}
nickto/PensionFundsLv documentation built on May 23, 2019, 5:08 p.m.