1

I would like to produce a pdf of a chart produced with quantmod. For example,

library(quantmod)

data(sample_matrix)
d <- as.xts(sample_matrix)

pdf("chart1.pdf")
chartSeries(d$Open,TA=c(addTA(d$Close,on=1),addTA(d$High)))
dev.off()   

The question is whether it is possible to produce a single-page pdf, like chart1.pdf, but using addTA() incrementally. To illustrate, the following code will create a three-page pdf, chart2.pdf; I would like to directly create the final page of that pdf, but without specifying the addTA calls in the original call to chartSeries as above.

pdf("chart2.pdf")
chartSeries(d$Open)
print(addTA(d$Close,on=1))
print(addTA(d$High))
dev.off()
Rahul Savani
  • 872
  • 1
  • 10
  • 24

1 Answers1

3

You can create your chart in as many steps as you want, then use dev.copy to create a pdf of it.

library(quantmod)

data(sample_matrix)
d <- as.xts(sample_matrix)

chartSeries(d$Open)
addTA(d$Close,on=1)
addTA(d$High)
dev.copy(pdf, "chart2.pdf")
dev.off()
GSee
  • 48,880
  • 13
  • 125
  • 145
  • Thanks, but that produces an empty pdf for me. Does it work for you? – Rahul Savani Sep 01 '13 at 21:22
  • @RahulSavani Yes it worked for me. Did you run the `dev.off()` line? – GSee Sep 01 '13 at 22:11
  • I did, but I suppose the problem was I had not run ``dev.off()`` previously and had some device still open. It worked fine in a clean workspace, thanks. – Rahul Savani Sep 02 '13 at 06:03
  • It doesn't seem to work from a script file called with Rscript. Any idea what to do for that case? – Rahul Savani Sep 02 '13 at 14:56
  • @RahulSavani [wrap the addTA calls in plot](http://stackoverflow.com/a/18342756/967840) – GSee Sep 02 '13 at 16:46
  • They are wrapped in plots (which is anyway needed where I source the script with ``source`` in which case it works). Doesn't work for. Here's the complete code: ``library(quantmod); data(sample_matrix);d <- as.xts(sample_matrix); chartSeries(d$Open);plot(addTA(d$Close,on=1));plot(addTA(d$High));dev.copy(pdf,"chart.pdf");dev.off()``. Thanks for your patience! – Rahul Savani Sep 02 '13 at 17:54