简体   繁体   English

R tcltk响应gui在计算期间

[英]R tcltk responsive gui during calculation

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. 我目前正在研究一个用于统计计算的R项目,该项目涉及gui和耗时的启发式算法。 in the gui shall be an button to start and stop the calculation and a textfield that reports the best error so far. 在gui中应该是一个启动和停止计算的按钮,以及一个报告迄今为止最佳错误的文本字段。

so i'm stuck with the question how to keep the gui responsive during the calculation. 所以我坚持在计算过程中如何保持gui响应的问题。

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. 在do stuff函数中,一些多线程的东西似乎是必要的。 its a requirement to work on windows and linux. 它需要在Windows和Linux上工作。 i'm hoping for ideas how to archive this. 我希望有关如何存档的想法。 thanks in advance 提前致谢

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. 我认为最简单的方法是使用带有wait=FALSE参数的systemsystem2启动R脚本作为后台进程,以便在后台处理长时间运行的计算。

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. 如果要更新UI以显示中间结果或进度,R脚本必须将当前状态写入可用于更新UI的“交换”文件。

To update the UI you have to check for the new state periodically eg by using the after command of Tcl/Tk, see: 要更新UI,您必须定期检查新状态,例如使用Tcl / Tk的after命令,请参阅:

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

For an example of starting an R script as background process see here: 有关将R脚本作为后台进程启动的示例,请参阅此处:

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. 请注意,R是单线程的,并且必须从创建UI的同一线程(进程)更新Tk-UI,因为主事件循环在此处运行。

Also take care that you shouldn't allow the user to make conflicting changes via the UI while the background task is running (eg starting another background task - except you want to support this). 另外请注意,在后台任务运行时,不应允许用户通过UI进行相互冲突的更改(例如,启动另一个后台任务 - 除非您想支持此操作)。

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. 通过UI取消后台进程(“取消按钮”)可以通过使用由后台进程定期检查的另一个“信号文件”来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM