Question

Assume that symbols is a character vector of stock symbols. After using getSymbols over the entire symbols vector I have them loaded in my workspace. To limit requests to Yahoo! servers I want to save these xts zoo objects in a files using the following code:

# Save to local drive
e = ".xts"
for (i in 1:length(symbols)){
  tempXTS = get(symbols[i]) # <= QUESTION relates to this line
  save(tempXTS, file = paste(dataFLD, Sys.Date(), symbols[i], e, sep = ""))
}
# Load from local drive
for (i in 1:length(symbols)){
  filepath = paste(getwd(), "/DATA/2013-12-21 ", symbols[i], ".xts", sep = "")
  load(filepath)
}

Question: I originally used tempXTS only for readibility of the code, but ran into issues when trying to load the objects from the .xts files. It seems the saving renames the object from say AAPL to tempXTS. The problem is, when I delete that line and substitute tempXTS wiht get(symbols[i]) in the next line I get this error (note dataFLD is the path to the data folder):

Error in save(get(symbols[i]), file = paste(dataFLD, Sys.Date(), symbols[i], : object ‘get(symbols[i])’ not found

Was it helpful?

Solution

There is no reason to assign get(symbol[i]) to a temporary object, or to even use get in the first place. As it says in ?save, you can use list= to specify the name of an object to save.

e = ".xts"
for (sym in symbols){
  save(list = sym, file = paste0(dataFLD, Sys.Date(), sym, e))
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top