[R] gWidgets process management

Peter McMahan mcmahan at uchicago.edu
Wed Feb 13 19:43:58 CET 2008


One more note on stopping at user click. The following is a bit of a  
hack but it seems to work. (Note: In the slow function I used "for(i  
in 1:5000000){}" instead of "Sys.sleep(1)" to better simulate a real  
slow R-only function):

reallySlowFunction <- function(n=10){
     for(i in 1:n){
         cat("z")
         while(gtkEventsPending()){
             gtkMainIteration()
             if(svalue(cbutton)=="stopping..."){
                 cat("interrupted")
                 svalue(cbutton) <- "stop simulation"
                 return()
             }
         }
         for(i in 1:5000000){}
     }
     cat("\n")
}

w <- gwindow("test")
g <- ggroup(cont=w, horizontal=FALSE)
b <- gbutton("click me", cont=g,handler =  
function(h,...)reallySlowFunction())
cbutton <- gbutton("stop simulation",cont=g,handler=function(h,...) 
{svalue(h$obj) <- "stopping..."})
r <- gradio(1:3, cont=g, handler = function(h,...) print(svalue(h$obj)))


Not very robust but a useful workaround. (I was hoping to avoid using  
RGtk2-specific code in the "reallySlowFunction", but I can add an  
extra argument to conditionally check if it's called in "interactive  
mode")

Thanks again to everybody for their help.

Peter


On Feb 13, 2008, at 11:18 AM, Peter McMahan wrote:

> Thanks for that link to the mac Gtk2, it's been very helpful.
>
> To work around my original problem I've decided to just have the gui
> be a seperate function that returns the parameter values entered. Then
> I'll call that function from within a non-gui function -- so have the
> window close and then the process start, all in a while loop.
> This won't allow for a button to stop the simulation, but it will do.
> Thanks for all your help everybody.
>
> Peter
>
> On Feb 12, 2008, at 1:56 PM, Michael Lawrence wrote:
>
>> On Feb 12, 2008 1:51 PM, Peter McMahan <mcmahan at uchicago.edu> wrote:
>>
>>> Thanks, that's very helpful. Unfortunately Gtk2 is difficult to get
>>> running on a Mac, so I've been trying the gWidgetstcktk interface.
>>> It sounds like the behavior you're describing is exactly what I  
>>> want,
>>> so it may just be a difference in the TGtk2 and tcltk event loops?
>>> In your example, can you think of a way to have a "cancel" button
>>> that
>>> would be able to kill reallySlowFunction() early?
>>> for the time being, I guess I'll just work on getting the gtk28
>>> package from macports working...
>>
>>
>> There are GTK+ binaries available that should work with the RGtk2  
>> CRAN
>> binary for the Mac.
>>
>> http://r.research.att.com/gtk2-runtime.dmg
>>
>>
>>> Thanks,
>>> Peter
>>>
>>> On Feb 12, 2008, at 1:31 PM, John Verzani wrote:
>>>
>>>> Dear Peter,
>>>>
>>>> I think this issue has more to do with the event loop than  
>>>> gWidgets.
>>>> I've cc'ed Michael Lawrence, who may be able to shed more light on
>>>> this. Perhaps gIdleAdd from RGtk2 can work around this, but I  
>>>> didn't
>>>> get anywhere. My understanding is that the event loop is preventing
>>>> the console from being interactive, but not GTK events. So for
>>>> instance, the GUI in the following example is responsive, during  
>>>> the
>>>> execution, but the command line is not.
>>>>
>>>> library(gWidgets)
>>>> options("guiToolkit"="RGtk2")
>>>>
>>>> reallySlowFunction = function(n=20) {
>>>> for(i in 1:n) {
>>>>  cat("z")
>>>>  Sys.sleep(1)
>>>> }
>>>> cat("\n")
>>>> }
>>>>
>>>>
>>>> w <- gwindow("test")
>>>> g <- ggroup(cont=w, horizontal=FALSE)
>>>> b <- gbutton("click me", cont=g,handler = function(h,...)
>>>> reallySlowFunction())
>>>> r <- gradio(1:3, cont=g, handler = function(h,...) print(svalue(h
>>>> $obj)))
>>>>
>>>> ## you can click the radio button and get a response, but not the
>>>> console
>>>>
>>>>
>>>> --John
>>>>
>>>>
>>>> Hello,
>>>> I'm trying to make a graphical interface for an R function
>>>> I've written. A common use for the function is to call it with
>>>> specific parameters, and then watch the output as it evolves.
>>>> There's not necessarily a logical stopping point, so I usually
>>>> use ctrl-C when I'm done to stop it.
>>>> I've made a gWidgets interface to the function that gets some
>>>> user info and then on a button click calls the function. The
>>>> gWidgets window, however, seems to be frozen as long as the
>>>> function is running, and the function output seems to be
>>>> happening in the "background" in my R session, so ctrl-C (esc
>>>> on the Mac GUI) does not work to stop it. I have to kill R
>>>> entirely to stop the process.
>>>> So after all that setup here is my question:
>>>> Is there some way to have a gWidgets window interrupt a
>>>> function that it has called via a widget handler?
>>>> Thanks,
>>>> Peter
>>>>
>>>> --
>>>> John Verzani
>>>> CUNY/CSI Department of Mathematics
>>>> verzani at math.csi.cuny.edu
>>>
>>> ______________________________________________
>>> 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.
>>>
>
> ______________________________________________
> 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.



More information about the R-help mailing list