R/getFundData.R

#' Get fund data.
#'
#' \code{getFundData} loads the fund 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 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 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 Data frame, with the fund data.
#'
#' @source Data comes from \url{http://manapensija.lv}.
#'
#' @seealso \code{\link{downloadFundData}}.
getFundData <- function(file = paste0("fund-data-", Sys.Date(), ".RData"),
                        dir = tempdir(),
                        force = FALSE,
                        quite = FALSE) {

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

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

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

  load(filePath)

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