0

In R,

I have 1 year of csv timeseries data in format DateTime and Close. I want to plot 1 day graphs of each day and then export them, ideally having the file name be the date of the graph.

The steps in this link http://www.r-bloggers.com/automatically-save-your-plots-to-a-folder/ are very helpful and are what I will use, but I need help figuring out how to automate the plotting of one day graphs first.

Currently using chart_Series to chart and then zoom_Chart to focus on the dates I want, and then manually exporting.

I am doing

spxxts <- xts(spx$Close, order.by = spx$DateTime)
chart_Series(spxxts)

and then

zoom_Chart("2007-04-30")
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
hrhyrn
  • 1
  • 2
  • 3
    It will be easier to help you if you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Kara Woo Aug 19 '15 at 01:22
  • 1
    So, what specifically is your question? Is there a problem with the code you have tried (if so, what is it)? Is it purely that you need to know how to write a loop over the dates? A reproducible example would be fantastic. – mathematical.coffee Aug 19 '15 at 01:36
  • 1
    You can utilize the `subset=` argument of `chart_Series()` so that you don't have to use `zoom_Chart()`. To save the charts to disk, you could use the `png()` function then `chart_Series()` then `dev.off()`, or you could use `dev.copy()` – GSee Aug 19 '15 at 13:30
  • Yes I want to know how to write a loop, that would do the subset function and then also save the charts to disk via the functions outlined above^. – hrhyrn Aug 20 '15 at 02:48
  • How could I write a function that would do subset= and then save each subsequent day? – hrhyrn Aug 25 '15 at 12:46

1 Answers1

0

Here is some code that will loop through the dates in your data and save the charts via: saveChart() by quantmod but you can change it to whatever you use to save charts:

# Required data
spxxts <- xts(spx$Close, order.by = spx$DateTime)

# Creates a Vector of all the ACTUAL days in your data (trading days)
INDX <- unique(as.Date(index(spxxts))) 


# Loop
for(ii in 1:length(INDX)){

  # plot:
  chart_Series(spxxts[paste(INDX[ii])], name=paste0(",INDX[ii]),"")

  # save:
  saveChart("jpeg")

}

the saveChart function will use the name of the chart & make that the name of the .jpeg and save it to your working directory... as requested the name of the chart is the trading date

Rime
  • 912
  • 2
  • 11
  • 39
  • when i run it it gives this error message? Error: unexpected string constant in: " # save: saveChart("" > } Error: unexpected '}' in " }" – hrhyrn Sep 01 '15 at 23:59
  • also it gives unexpected token error for the end parentheses in this INDX[ii]),"") – hrhyrn Sep 02 '15 at 00:33