如何停止 R gWidgets 脚本退出
我正在使用 gWidgets 工具包在使用 Rscript 运行的 R 脚本中创建 GUI。
创建 GUI 后,脚本退出。
我可以在脚本末尾使用 while(TRUE){Sys.sleep(9999)} 循环来防止这种情况,但这看起来很糟糕。
有没有更好的方法告诉 R 仅在 GUI 关闭时退出,或者至少在 GUI 构建后进入 REPL?
I am using the gWidgets toolkit to create a GUI in an R script that is run using Rscript.
When the GUI is created, the script exits.
I can prevent this with a while(TRUE){Sys.sleep(9999)} loop at the end of the script but that seems hacky.
Is there a better way of telling R to exit only when the GUI is closed, or at least to enter the REPL once the GUI is constructed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您也许可以根据您的需要调整 gbasicdialog。此构造函数创建一个模态容器,您可以从中生成其他窗口。这是一个示例:
“tcltk”工具包也是如此。它几乎使用了格雷格建议的方法。
You might be able to adapt gbasicdialog for your needs. This constructor creates a modal container from which you can spawn other windows. Here is an example:
The same works for the "tcltk" toolkit. It uses pretty much what Greg suggests can be done.
这个主题可能已经结束了,但作为小部件的新手,我曾经遇到过。 jverzani给出的解决方案显然是一个解决方案。我选择了另一个,没有使用任何补充对话框,只是因为我不想要一个,根本没有其他原因...
在 gwindow 的处理程序中,处理后我从环境中删除了变量:
handler = function(h,...) {dispose(EDFAnalysis$w); rm(w,envir=EDFAnathesis)}
其中
EDFAnaanalysis
是我的脚本的环境...而w
是主 gwindow。然后,在我的脚本末尾添加:
while(exists("w",EDFAnalysis)){Sys.sleep(5)}
当然,可以使用小于 5 的值或更大的值。就我而言,5 秒就足够了,但不是永远......:-)
This subject may be closed but as a newbie to gwidgets, I have been confronted with. The solution given by jverzani is obviously a solution. I have chosen another one, not using any supplementary dialog, just because I don't want one, no other reason at all...
In the handler of the gwindow, after disposal I remove the variable from the environment:
handler = function(h,...) {dispose(EDFAnalysis$w); rm(w,envir=EDFAnalysis)}
where
EDFAnalysis
is the environment of my script... andw
is the main gwindow.Then, at the end of my script I added:
while(exists("w",EDFAnalysis)){Sys.sleep(5)}
of course, smaller value than 5 or greater value can be used. In my case, 5 s is sufficient and not for ever... :-)
处理此问题的标准方法是请求用户输入以继续。这一句就可以解决问题。
编辑:
readline
仅在交互式使用下有效,因此我将其替换为scan
,后者有点不太漂亮。所以你的脚本应该看起来像
The standard way of dealing with this is to request user input to continue. This one-liner will do the trick.
EDIT:
readline
only works under interactive use, so I've swapped it forscan
, which is a little less pretty.So you script should look like
如果您使用
tcltk
包而不是gWidgets
,那么您可能会使用tkwait.window
函数code>tcltk 告诉脚本等待 gui 窗口消失后再继续执行脚本。If you are using the
tcltk
package instead ofgWidgets
then you could possibly use thetkwait.window
function fromtcltk
to tell the script to wait until the gui window goes away before continuing the script.我发现,一个好方法是使用 RGtk2 库中的 gtkMain() 函数。这只是让主循环保持运行,直到调用
gtkMainQuit()
为止。A good way to do it, I've found, is to use the
gtkMain()
function in the RGtk2 library. This simply keeps the main loop running untilgtkMainQuit()
is called.为了完整起见:ozjimbob 已经给出了最“干净”的方法的答案。
ffeschet 的答案对我不起作用,无论是在 Unix 还是 Windows 上。
因此,在主“启动”脚本中,您至少必须具有以下条目:
在“子”进程“StartMyGUI()”中,您的代码可能如下所示:
仅当用户点击“取消”时将调用 gtkMainQuit() 的按钮,该按钮退出主“启动”脚本中的母进程。
For completeness: ozjimbob already gave the answer for a most "clean" way how to do it.
The answer of ffeschet did not work with me, neither on Unix nor on Windows.
Hence, in the main "launching" script, you have to at least have these entries:
In the "child" process "StartMyGUI()", your code could e.g. look like this:
It is only when the user hits the "Cancel" button that gtkMainQuit() will be called, which exits the mother process in the main "launching" script.