1

I have a chart created inside a couple of loops and I want to automatically write the chart to a file at the end of the outer loop. Here is a toy example:

filename <- "mychart"
for(i in 1:5) {
  x <- 1:5
  fun1 <- sample(1:10, 5, replace = TRUE)
  xlim <- c(1, 5)
  ylim <- c(0, 10)
  plot(x, fun1, xlim = xlim, ylim = ylim, type = "l")
  for(j in 1:3)  {
    fun2 <- 2:6 + j
    lines(x, fun2, type = "l", col = "red")
  }
  out.filename <- paste(filename, i, sep = "")
  ## want to save this plot out to disk here!
}

I would also like to create the plot on the console so I can watch the program’s progress. Most answers to a similar question seem to deal with a plot that is created with a single “plot” statement, or do not enable the console plot window. Any suggestions much appreciated.

smci
  • 32,567
  • 20
  • 113
  • 146
Ernie
  • 1,103
  • 1
  • 14
  • 25
  • 1
    Maybe this previous answer of mine can be useful, almost a duplicate I guess. [Here](http://stackoverflow.com/questions/30835815/save-imported-csv-data-in-vector-r/30835924#30835924) the link. – SabDeM Aug 18 '15 at 18:17
  • Yes, savePlot is what I need. Example given below. Thanks. – Ernie Aug 18 '15 at 21:40
  • @smci this is base graphics, there is no plot object. – Gregor Thomas Aug 19 '15 at 00:12

2 Answers2

1

I think this does what you're after:

plotit <- function(i) {
   x = 1:5
   fun1 = sample(1:10, 5, replace=TRUE)
   plot(x, fun1, xlim=c(1,5), ylim=c(0,10), type="l")
   for(j in 1:3)  {
       fun2 = 2:6 + j
       lines(x, fun2, type = "l", col = "red")
   }   
   savePlot(paste0("mychart", i, ".png"), type="png")
}

Then:

for(i in seq(5)) plotit(i)
ulfelder
  • 5,305
  • 1
  • 22
  • 40
  • Thanks. This throws an error because I think I need to load an X11 library to use savePlot. Error says "Error in savePlot(paste0("mychart", i, ".png"), type = "png") : can only copy from 'X11(type="*cairo")' devices “ and I am using RStudio which may be a factor. – Ernie Aug 18 '15 at 21:39
  • OK. The more I dig into this .... I am using a Mac OS and RStudio. When I try to use X11( ) I need to install the package, XQuartz. But, XQuartz is not available for R3.2.1. So, maybe savePlot is not available to me. ???? – Ernie Aug 18 '15 at 21:52
  • 1
    OK. To make the answer above work with Mac OS (Maverick) and RStudio I had to install XQuartz and make it my primary X11 window server. This adds a layer of complexity to my desktop just to solve this one issue, but it does work. – Ernie Aug 18 '15 at 22:34
  • Yes the RStudio procedure for installing XQuartz is a bit painful, perhaps you can suggest improvements to their error messages and online doc. – smci Aug 18 '15 at 23:41
  • Apologies for suggesting a Windows-specific answer without clarifying that it was, and I'm glad you got it sorted. – ulfelder Aug 19 '15 at 08:29
0

The typical way to save base graphics plots is with individual device functions such as pdf(), png(), etc. You open a plot device with the appropriate filename, create your plot, then close the device with dev.off(). It doesn't matter if your plot is created in a for loop or not. See lots of devices (and examples at the bottom) in ?png.

For your code, it would go something like this:

filename <- "mychart"
for(i in 1:5) {

    out.filename <- paste(filename, i, ".png", sep = "")
    ## Open the device before you start plotting
    png(file = out.filename)
    # you can set the height and width (and other parameters) or use defaults

    x <- 1:5
    fun1 <- sample(1:10, 5, replace = TRUE)
    xlim <- c(1, 5)
    ylim <- c(0, 10)
    plot(x, fun1, xlim = xlim, ylim = ylim, type = "l")
    for(j in 1:3)  {
        fun2 <- 2:6 + j
        lines(x, fun2, type = "l", col = "red")
    }
    ## Close the device when you are done plotting.
    dev.off() 
}
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • But, Gregor, that doesn’t allow one to see the plot in the RStudio console window, right? It would be preferable to be able to see these plots as they are being made and not have to wait and look at the files, because the real loops I am using run for hours. – Ernie Aug 19 '15 at 00:52
  • No, it doesn't. (guess I didn't read the question all the way through :\ ). You can look at each plot as soon as the `dev.off()` line is run, though. Are you really spending hours watching plots get made and then saved? Seems like saving them all and reviewing is a better workflow. – Gregor Thomas Aug 19 '15 at 01:48
  • No, agree, but I need to look at the first one or maybe two plots to make sure things are going OK before I invest hours of machine time in the entire run. There may be things I need to correct by interrupting the run based on the first plot. But, your solution is a bit cleaner without involving X11. I may just go with it and, as you say, peek at the first plot file while the program is still running. – Ernie Aug 19 '15 at 02:10