Skip to content

Instantly share code, notes, and snippets.

@tmasjc
Last active June 27, 2018 16:29
Show Gist options
  • Save tmasjc/2fc32cfbac74466b195ae1fa673e1f5d to your computer and use it in GitHub Desktop.
Save tmasjc/2fc32cfbac74466b195ae1fa673e1f5d to your computer and use it in GitHub Desktop.
Iterate over x rates to scrap currency. #purrr
# Reference
# http://www.x-rates.com/average/?from=CNY&to=EUR&amount=100&year=2017
library(rvest)
#
base <- "http://www.x-rates.com/average/?amount=100"
# Currency
cur <- c("USD", "CNY")
year_range <- 2017:2018
# Permutation of above currency
cur_df <- expand.grid(cur, cur) %>%
# remove same-currency pair
filter(Var1 != Var2) %>%
# make into data frame
mutate(url = paste0(base, "&from=", Var1, "&to=", Var2, "&year="))
head(cur_df)
# Iterate over multiple year to produce list
urls <- purrr::map(year_range, ~paste0(cur_df$url, .))
urls
# single case
url = "https://www.x-rates.com/average/?amount=100&from=JPY&to=CNY&year=2015"
xml %>% html_nodes(".avgRate") %>% html_text() %>% list()
# obtain 12-month average
get_avg_rate <- function(url){
url %>% read_html() %>% html_nodes(".avgRate") %>% html_text() %>% list()
}
# a safer approach
get_safely <- safely(get_avg_rate)
# urls %>% map( ~ map(., get_safely))
get_from_list <- function(url_list){
map(url_list, get_safely)
}
map(urls, get_from_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment