Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a series of plots that proceed by a click

Tags:

r

In the example below I would like to be able to control when I go to the next plot by a using mouse click (or keyboard entry)

for (i in 1:5){
    plot(1:i)
    Sys.sleep(1)
    #add something here that requests mouse click to proceed
}

Is this possible? There is a setting in the X11() help file caled 'clickToConfirm' but I can't work out what that does.

It would also be helpful to me to be to be able to scroll back and forth through plots using the arrow keys. Is this possible?

Currently if I need to look at lots of plots I output them into a big .pdf file and scroll though them all there, but that is a bit cumbersome.

Thanks

Tom

like image 867
Tom Liptrot Avatar asked May 17 '11 12:05

Tom Liptrot


2 Answers

In R, that would be done by setting par(ask=TRUE). Try the following code, which shows how to reset the par when exiting the function :

op <- par(ask=TRUE)
for (i in 1:5){
    plot(1:i)
}
par(op)

If you want to keep a history to browse through, you can either open a window and click on recording in the History menu, or you can open the window yourself with the history on. Demonstrated in a function :

plot.fun <- function(){
    windows(record=TRUE) # opens a window and starts recording
    op <- par(ask=TRUE)
    on.exit(par(op))

    for (i in 1:5){
        plot(1:i)
    }
    windows.options(record=FALSE) #stops recording.
}
plot.fun()

This will however keep all previous plots in the history for browsing as well, so if you run this code 3 times you'll have 15 plots in the plot history. Also note that the open plot window will keep on recording until you turn off the recording in the menu.

You can play with the plot history, as you'll have a variable .SavedPlots which contains the saved plot history. It can be cleared using the menu History > clear history in the plot window. If you want to clear the history from the console, you could hack that by

.SavedPlots <- NULL 

But I advise you not to do this, as changing the .SavedPlots variable can cause R to crash.

See also ?windows and ?recordPlot for a bit more information. But as you're getting close to the internal code of R, be warned that you can get pretty awkward behaviour if you start playing around with these things.

like image 118
Joris Meys Avatar answered Oct 02 '22 19:10

Joris Meys


For scrolling back and forth between plots using the arrow keys: it depends on the platform/R interface.

  • Windows: there is a recording function (see Q5 of the R for Windows FAQ) which uses Page Up/Page Down
  • MacOS: under the standard GUI, the Quartz window has Apple-left and Apple-right arrow
  • under the standard Unix (no-GUI) interface, things are more limited. You can use RStudio (which has a lot of buzz right now) ... I would have thought that JGR would have plot history as well, but it doesn't seem to ...
like image 26
Ben Bolker Avatar answered Oct 02 '22 18:10

Ben Bolker