2

I'm trying to execute a for-loop inside a r notebook chunk to print some messages and a plot interleaved, shown in the code below:

for(i in 1:2) {
  print(paste0("before ", i))
  plot(i, i)
  print(paste0("after ", i))
}

However, the first plot appears where the second should have been and the second one after all messages have been printed. Looking for a solution I found this (Using loops to print output into Knitr)

for(i in 1:2) {
  print(paste0("before ", i))
  plot(i, i)
  plot.new()
  # This function (frame is an alias for plot.new) causes the completion of 
  # plotting in the current plot (if there is one) and an advance to a new
  # graphics frame.
  print(paste0("after ", i))
}

Which kinda fix it, but unfortunately empty plots are created for each valid plot. Does anyone know a method that causes the completion of plotting without creating a new graphic frame?

Community
  • 1
  • 1
  • `plot.new` _after_ your `plot` statement just blanks out what you have done. I think that you want it before the `plot`. Also, do you want `plot.new` or `dev.new`? – G5W Jan 13 '17 at 16:29
  • No, it doesn't. It flushes the previous plot and creates an extra blank plot. I just really need the flush part. – Frederico Nogueira Jan 13 '17 at 17:24

0 Answers0