Last active
August 19, 2019 08:22
-
-
Save cpsievert/e05da83fc4253e6d1986 to your computer and use it in GitHub Desktop.
Convenience functions for https://openexchangerates.org/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (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