0

If I run the code below, I am not able to see the plots change as the for-loop gets executed very quickly.

for(i in 1:10) {
    plot(rnorm(20))
}

I am wondering if there is a way to slow down the for-loop and create one plot every 5 seconds. Thanks!

Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
Filly
  • 713
  • 12
  • 23
  • 1
    Rather than pausing, you can save the figures you produce using `pdf(...); plot(...); dev.off()` sequence, where you generate the name of the file by doing something like `t_plot_name <- paste0("myPlot_", i); pdf(file=t_plot_name, ...)`. Alternatively, make use of the `par(mfrow=c(3,4))` (e.g.) call to get a figure that has 12 panels in a 3x4 arrangement. – rbatt Dec 08 '15 at 17:27

1 Answers1

1

You can wait for user input before showing the next plot:

# Wait for user input before showing next plot
par(ask=TRUE) 

# Loop that makes plots
for(i in 1:10) {
  plot(rnorm(20))
}

To actually wait for 5 seconds between plots:

# Loop that makes plots
for(i in 1:10) {
  plot(rnorm(20))
  # Wait 5 seconds
  Sys.sleep(5)
}
ialm
  • 8,510
  • 4
  • 36
  • 48