2

I was wondering how I could lower the speed (i.e., the timing) of the for() loop below to allow plot changes to be visible and tangible by human eye in R (without having to click enter and just watching the looping run slowly)?

Here is a piece of R code:

for (i in 1:50) {

  plot(rnorm(40))
  readline(prompt="Press [enter] to continue")

}
rnorouzian
  • 7,397
  • 5
  • 27
  • 72
  • 1
    Add `Sys.sleep(1)` instead of `readline` – David Arenburg Apr 12 '17 at 21:30
  • 1
    You could wrap the loop with `pdf()` to make a slideshow/flipbook... that's what I do. – Frank Apr 12 '17 at 21:39
  • @Frank, could you please show me frank? – rnorouzian Apr 12 '17 at 21:40
  • You put `pdf(file = some_file_path)` before, and `dev.off()` after. I have an example in 3.2.4.4 here, though you'd need the data.table package and to be careful about what folder you run it in for that http://franknarf1.github.io/r-tutorial/_book/tables.html#extensions-to-the-data.frame-class The `DT[, {do_stuff}, by=g]` there is just like your for loop ... it just happens to be looping over subsets of a table instead. – Frank Apr 12 '17 at 21:43
  • 1
    You could use Yihui's [animation](https://github.com/yihui/animation) package to turn them into a GIF or video if you like. – alistaire Apr 12 '17 at 21:47
  • @alistaire In that light, maybe this is a dupe: http://stackoverflow.com/questions/1174799/how-to-make-execution-pause-sleep-wait-for-x-seconds-in-r/1174826 ? – Frank Apr 12 '17 at 21:48
  • @parvinkarimi Oh, here's a better illustration: http://stackoverflow.com/a/1395437/ – Frank Apr 12 '17 at 21:51
  • @Frank, that is really wonderful! Could I instead make png? – rnorouzian Apr 12 '17 at 21:54
  • Yes. With a pdf, you could put in on multiple pages, while with a png, you'd fill a folder with images (since pngs don't have pages). In the docs `?png` see the `filename` argument for how to specify a pattern to make file names. (I haven't used it that way myself.) – Frank Apr 12 '17 at 21:57
  • 1
    @Frank, sure, thank you. – rnorouzian Apr 12 '17 at 21:58
  • @Frank, I am trying to get a pdf file in the fashion that you suggested for a fairly large plot. Would the size of the plot be a problem? – rnorouzian Apr 18 '17 at 17:48
  • @parvin It might be. Scatterplots render in a costly way, whether in a pdf or in an R window. If you're running into that problem, maybe best to look at the hexbin package or something else that coarsens or smooths a scatter plot. I guess it might be an issue for other plotting types as well, but I'm not very familiar. – Frank Apr 18 '17 at 17:56
  • @Frank, can I send you an R file before asking a question, everything works fine except when I want to get a png file for each "i", the png files come out blank? – rnorouzian Apr 18 '17 at 18:20
  • Nah, sorry. I'm no graphing guru. I only make graphs a couple times a year and always use pdf, base plotting functions, etc. – Frank Apr 18 '17 at 18:23

1 Answers1

3
pauseLength = 4   # length of pause in seconds

for (i in 1:50) {
    plot(rnorm(40))
    Sys.sleep(pauseLength)
}
steveb
  • 5,382
  • 2
  • 27
  • 36
brb
  • 1,123
  • 17
  • 40