Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an R function that displays plots sequentially?

I want something similar to the 'plot.lm' functionality.

y <- rnorm(100, 0, 1)
x <- rnorm(100, 0, 1.5)

mod <- lm(y ~ x)

plot(mod)

Hit <Return> to see next plot: 
Hit <Return> to see next plot: 
Hit <Return> to see next plot: 
Hit <Return> to see next plot:

I want to create several plots and then display them sequentially - prompting the user to hit in order to see all the plots.

like image 439
Frank P. Avatar asked Sep 07 '25 08:09

Frank P.


2 Answers

Just set par(ask=TRUE) before calling plot(). You might want to set it after your first plot, so the user doesn't have to wait for that one. To be nice, set par(ask=FALSE) after your last plot.

like image 117
Jthorpe Avatar answered Sep 11 '25 12:09

Jthorpe


A simpler solution for this that I've found is what plot.lm() does somewhere in the beginning of the function:

oask <- devAskNewPage(TRUE);
on.exit(devAskNewPage(oask));
like image 22
Ivan Svetunkov Avatar answered Sep 11 '25 13:09

Ivan Svetunkov