I think that the problem is with the dev.off commands.
The first time running b there are not a device 2 and device 3 so
dev.off does nothing (or if they did exist then they are turned off).
Then you set the graphics event callback which attaches to the current
graph window (number 3 in general). Then when you call b again, this
graphics device goes away and since the callback is attached to that
device, the callback goes away as well and either that stops the
execution, or things just get confused.
I would recommend instead of using dev.off, use dev.set with dev.prev
and dev.next to move between active windows (create the 2 windows the
first time, but just move between them after that, don't delete and
create new ones). That should help, and will also work better if there
are multiple open devices before calling a.
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of
> cgenolin at u-paris10.fr
> Sent: Sunday, April 13, 2008 6:49 AM
> To: r-help at r-project.org
> Subject: [R] getGraphicsEvent plotting two graph
>
> Hi the list,
>
> I need to use getGraphicsEvent to plot two graph. On the following toy
> example:
> - the function b is ploting two graph depending on a parameter
> - the function a is calling b according to some user answers.
> It is suppose to call b until the user press Return, but it does not.
>
>
> b <- function(x){
> dev.off(2);dev.off(3)
> windows();windows(3,3,xpos=0)
> dev.set(2);plot(1:x)
> dev.set(3);plot(1:x^2)
> return(NULL)
> }
> b(2)
>
> a <- function(y){
> b(y)
> getGraphicsEvent("Arrow Up or Return",
> onKeybd=function(key){
> if(key!="ctrl-J"){
> if(key=="Up"){y <<- y+1}else{}
> b(y)
> cat("\n*** This function a never gets here...
***\n")
> return(NULL)
> }else{
> return(TRUE)
> }
> }
> )
> }
>
> a(3)
>
> Is there something wrong in my code, or something I did not
> understand in getGraphicsEvent ?
> I try on R 2.2.1, then on R 2.7
>
> Thanks for your help.
>
> Christophe
>
> ----------------------------------------------------------------
> Ce message a ete envoye par IMP, grace a l'Universite Paris
> 10 Nanterre
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>