Search code examples
rquantmod

Use a custom filename in quantmod saveChart function


In R quantmod, when use saveChart to save the plot ,how to assign file name for the result ?

Below code saveChart('my_plot.png', width=13) can't work

library(quantmod)
getSymbols("AAPL")
chartSeries(AAPL)
require(TTR)
addBBands()
saveChart('my_plot.png', width=13)

Solution

  • saveChart gets the filename from object name. Here are alternatives to get the custom filename:

    Set auto assign to FALSE, and name the data as you wish before plotting:

    my_plot <- getSymbols("AAPL", auto.assign = FALSE)
    chartSeries(my_plot)
    addBBands()
    saveChart(.type = "png")
    

    Use auto assign, then re-assign it to other object before plotting:

    getSymbols("AAPL")
    my_plot <- AAPL
    chartSeries(my_plot)
    addBBands()
    saveChart(.type = "png")
    

    Use png graphics device:

    png("my_plot.png")
    chartSeries(AAPL)
    addBBands()
    dev.off()