Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This script scrapes a current exchange rate and writes it to a csv file. It's meant to be automated using Linux crontab. To have it run every hour you would type something like this: ~$ crontab -e 0 * * * * sudo R CMD BATCH /home/shared/barug_oct13/get_conv_rate.R
# Clark Fitzgerald 23 Sep 13
#
# This script gets current exchange rates and writes to csv file.
csv.path <- "/home/shared/barug_oct13/hourly_rate.csv"
library("XML")
# We'll compare the conversion rate from US to South Korea.
country <- "korea"
# Read the tables in.
data <- readHTMLTable("http://www.x-rates.com/table/?from=USD&amount=1")
# Look at the larger table.
data <- data[[2]]
# We only want the country of our choice.
country.index <- grepl(country, data[, 1], ignore.case = TRUE)
# This is the rate we care about.
rate <- data[country.index, 2]
# Define the row to be written into our .csv file.
row <- data.frame(Sys.time(), rate)
# Write it into .csv file.
# Note that you cannot use write.csv with append = TRUE,
# so we use write.table.
write.table(row, csv.path, append = TRUE,
quote = FALSE, sep = ", ", row.names = FALSE, col.names = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment