2

I have a long list of xts objects and I'd like to automatically plot and save them to a folder (because it takes a v long time to do manually with >500 plots). The tricky part seems to be to apply functions to lists of xts objects. For example, getting a generalized naming method for the list elements based on its date (each xts object in the list is a unique day), and plotting each object and saving them to a file path with its own name.

The data can be found in a previous post here. Each xts element is a unique day in the list, with a price and volume column. I'm using the packages xts, TTR, and quantmod with the latest R (2.15).

I've tried this code to name the list elements, based on a great post on r-bloggers:

names(sample.data.uniquePOS) <- paste0("sample.data.uniquePOS", lapply(sample.data.uniquePOS, function(x) .indexday(sample.data.uniquePOS)))                                                    

This is supposed to name them by their day index (e.g. if 1st January 2012 then it would be "2012-01-01" as the element name). Unfortunately it doesn't work, producing a list of length sample.data.uniquePOS but each element is named sample.data.uniquePOSnumeric(0). I think the problem is the .indexday being applied to a list, when it should be to an xts object, but I'm not sure how to get around it.

The next step is to produce the file path to save the plots to, and then to do the plots:

mypath <- file.path("C:", "Documents and Settings", 
                              paste("Date_", names(sample.data.uniquePOS), ".jpg", sep = ""))

jpg(file=mypath)
mytitle = paste("my title is", names(sample.data.uniquePOS))
candleChart(sample.data.uniquePOS[[1]]:sample.data.uniquePOS[[length(sample.data.uniquePOS)]])
dev.off()
}

This has the same problem of candleChart needing to be applied to an xts object instead of a list:

Error in try.xts(x, error = "chartSeries requires an xtsible object") : 
  chartSeries requires an xtsible object

I'd really appreciate some help with this!

Community
  • 1
  • 1
Rothsom
  • 53
  • 7
  • `jpg` and `candleChart` are not vectorized. Use a loop over your list. – Joshua Ulrich Apr 04 '13 at 20:01
  • @JoshuaUlrich This loop seems to work for the candleChart - `plots1<- lapply(sample.data.uniquePOS, candleChart)`, but then `lapply(plots1, jpg(file=mypath)` (or `jpeg(...)`) returns the error `Error in match.fun(FUN) : jpeg(file = mypath) is not a function, character or symbol`. – Rothsom Apr 05 '13 at 10:03
  • @JoshuaUlrich Also tried this `lapply(names(sample.data.uniquePOS), function(x) dev.copy2pdf(filename=mypath), candleChart=sample.data.uniquePOS[[x]])` but it returned "list()", and not sure what that means. Also tried a loop: `for (i in 1:length(sample.data.uniquePOS)) {filename=paste("Main_", names(sample.data.uniquePOS), ".jpg", sep = "") jpeg(filename) candleChart}` but it created a single jpeg (when there should be as many jpegs as xts objects) with nothing in it. – Rothsom Apr 05 '13 at 14:37

0 Answers0