Is it possible to build a quantmod chart incrementally and export the final result as a single-page pdf?

StackOverflow https://stackoverflow.com/questions/18556548

  •  27-06-2022
  •  | 
  •  

Question

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()
Was it helpful?

Solution

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top