Convenience functions for https://openexchangerates.org/
# (1) Get a free plan from https://openexchangerates.org/signup/free | |
# (2) Tell this function your API key -- Sys.setenv("OER_KEY", "your-key-here") | |
getDay <- function(day) { | |
u <- sprintf( | |
"https://openexchangerates.org/api/historical/%s.json?app_id=%s", | |
day, Sys.getenv("OER_KEY") | |
) | |
res <- jsonlite::fromJSON(u) | |
# convert timestamp to date object | |
# http://stackoverflow.com/questions/13456241/convert-unix-epoch-to-date-object-in-r | |
res$rates$date <- as.POSIXct(res$timestamp, origin = "1970-01-01") | |
data.frame(res$rates) | |
} | |
getRates <- function(start = end - 3, end = Sys.Date()) { | |
days <- seq(start, end, by = "1 day") | |
Reduce("rbind", lapply(days, getDay)) | |
} | |
dat <- getRates() | |
dat[, c("AUD", "NZD", "date")] | |
#> AUD NZD date | |
#> 1 1.403416 1.528405 2015-11-13 10:00:00 | |
#> 2 1.402924 1.529136 2015-11-14 10:00:00 | |
#> 3 1.402924 1.529136 2015-11-15 10:00:00 | |
#> 4 1.402924 1.529136 2015-11-15 12:00:11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment