1

Hello dear stack overflow community.

i'm currently working on an R project for statistical calculations that involves a gui and also time consuming heuristics. in the gui shall be an button to start and stop the calculation and a textfield that reports the best error so far.

so i'm stuck with the question how to keep the gui responsive during the calculation.

some example code

require("tcltk")

result<-tclVar("")

start<-function(){
  active<<-TRUE
  tkconfigure(button,text="stop",command=stop)
  dostuff()
}

stop<-function(){
  active<<-FALSE
  tkconfigure(button,text="start",command=start)
}

dostuff<-function(){#this would be the optimization function
  while(active){
  tclvalue(result)<-#do some stuff
  }
}


toplevel<-tktoplevel()
button<-tkbutton(toplevel,text="start",command=start)
entry<-tkentry(toplevel,textvariable=result)
tkpack(button)
tkpack(entry)

in the do stuff function some multithreading stuff seems to be necessary. its a requirement to work on windows and linux. i'm hoping for ideas how to archive this. thanks in advance

hannes
  • 11
  • 3
  • Welcome to SO. Would it be possible for you to post the errors you receive as part of your question? – Richard Erickson Apr 04 '16 at 14:53
  • 1
    thanks. the problem is: when user clicks the button R starts to calculate the dostuff function and does not react to gui events anymore. the gui freezes and the user has no chance to stop the doStuff execution. so there has to be a seperation of the gui and the calculation process/thread and i'm asking for the best way to do this – hannes Apr 04 '16 at 15:09
  • Tcl/Tk and R are using the same execution thread ("process") that's why the UI freezes until the long running calculation has finished. I think you only have to start a new thread in `dostuff()` (I can't tell you how ATM). A special problem could be updating the UI inside of `dostuff` then because two different threads may cause strange side effects if they access the same UI... – R Yoda May 11 '16 at 09:44
  • Multithreading in R is discussed here: [http://stackoverflow.com/questions/10835122/multithreading-with-r] – R Yoda May 11 '16 at 14:18

1 Answers1

0

I think the easiest way is to start an R script as background process using system or system2 with the wait=FALSE parameter so that the long running calculation is processed in the background.

If you want to update the UI to show intermediate results or progress the R script must write the current state into an "exchange" file that can be used to update the UI.

To update the UI you have to check for the new state periodically e. g. by using the after command of Tcl/Tk, see:

http://www.tcl.tk/man/tcl/TclCmd/after.htm

For an example of starting an R script as background process see here:

http://stackoverflow.com/questions/14208976/r-run-source-in-background

Note that R is single threaded and updating the Tk-UI must be done from the same thread (process) that created the UI since the main event loop is running here.

Also take care that you shouldn't allow the user to make conflicting changes via the UI while the background task is running (e. g. starting another background task - except you want to support this).

Canceling the background process via the UI ("cancel button") can be done best by using another "signal file" that is checked by the background process periodically.

R Yoda
  • 8,358
  • 2
  • 50
  • 87