#' Download risk-free rate data.
#'
#' \code{downloadRfData} downloads the most recent risk-free rate data into the
#' specified directory and 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 maturity Numerical, maturity of yield in years.
#' @param start Date (or an object that can be parsed using \code{as.Date}),
#' start of the period.
#' @param end Date (or an object that can be parsed using \code{as.Date}),
#' end of the period.
#'
#' @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{getRfData}}, \code{\link{downloadFundData}}.
downloadRfData <- function(maturity,
file = paste0("rf-data-", Sys.Date(), ".RData"),
dir = tempdir(),
start = "2004-09-06",
end = Sys.Date()) {
# Error handling
if (!all(is.character(file),
is.character(dir),
length(file) == 1,
length(dir) == 1)) {
stop("file and dir should be strings")
}
filePathCsv <- paste0(tempfile(), ".csv")
filePathRdata <- paste0(dir, "/", file)
# Create folders if does not exist
if (!dir.exists(dir)) {
dir.create(dir)
}
# Download data to file
fileUrl <- generateRfUrl(maturity = maturity, start = start, end = end)
download.file(fileUrl, filePathCsv, mode="wb")
# Read data from downloaded file
rfData <- read.csv(filePathCsv, skip=5, header=F)
names(rfData) <- c("date", "rf")
# convert date to date format
rfData$date <- as.Date(rfData$date)
save(rfData, file = filePathRdata)
return(filePathRdata)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.