Funky Si's Tech Talk by Simon Foster
Learning R27 February 2017
softwaredevelopment development exchangerates programming
The problem I was trying to solve was to convert local prices of some items into Euros. I had been using a fixed exchange rate for all data, but as exchange rates fluctuate so much this is incorrect.
My first though was to find a free API that I could query to get the values I wanted. The first API I found didn’t cover all the currencies, the next one I found I burnt through the free allowance in one pass.
A colleague of mine mentioned using R to solve this, he sent me some links and I set out to write my first piece of R code.
My finished code can be found on github and I will attempt to explain some of it.
R defines functions fairly simply
nameoffunction <- function(arg1, arg2)
arg1 * arg2
I have created a function that takes 2 parameters date and currency. I know I have about 10 different currencies that I want to get currencies for and I want to loop through each day so I will need to pass in a date.
The source of my exchange rate information is the www.xe.com website, its historical exchange rate page passes currency and date into the query string so I should be able to build up a string containing all the different elements.
All programming language can concatenate strings and R is no different. It uses paste()
var <- paste(“Hello”, “World”)
However R has an annoying feature in this function. I would expect that var in the above example would contain “HelloWorld”, it doesn’t it contains “Hello World”. Why it automatically adds a space I don’t know?
var <- paste(“Hello”, “World”, sep=””)
I am not entirely sure what all of the code does but I can take a good guess.
read_html() I would guess loads a html page, html_nodes() finds all the html tags of a certain type on the page, in my case