Skip to content

Instantly share code, notes, and snippets.

@jaehyeon-kim
Created November 19, 2014 01:54
Show Gist options
  • Save jaehyeon-kim/356cf62b61248193db25 to your computer and use it in GitHub Desktop.
Save jaehyeon-kim/356cf62b61248193db25 to your computer and use it in GitHub Desktop.
download NASDAQ stock data
# assumes codes are known beforehand
codes <- c("MSFT", "1234")
urls <- paste0("http://www.google.com/finance/historical?q=NASDAQ:",
codes,"&output=csv")
paths <- paste0(codes,"csv")
missing <- !(paths %in% dir(".", full.name = TRUE))
missing
# simple error handling in case file doesn't exists
downloadFile <- function(url, path, ...) {
# remove file if exists already
if(file.exists(path)) file.remove(path)
# download file
tryCatch(
download.file(url, path, ...), error = function(c) {
# remove file if error
if(file.exists(path)) file.remove(path)
# create error message
c$message <- paste(substr(path, 1, 4),"failed")
message(c$message)
}
)
}
# wrapper of mapply
Map(downloadFile, urls[missing], paths[missing])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment