如何停止 R gWidgets 脚本退出

发布于 2024-12-11 06:13:17 字数 200 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

韬韬不绝 2024-12-18 06:13:17

您也许可以根据您的需要调整 gbasicdialog。此构造函数创建一个模态容器,您可以从中生成其他窗口。这是一个示例:

library(gWidgets)
options(guiToolkit="RGtk2")
require(fortunes)                       # just for fun

hold_it <- gbasicdialog(do.buttons=FALSE)
b <- gbutton("click me for a message", cont=hold_it, handler=function(h,...) {
  gmessage(paste(fortune(), collapse="\n"), parent=hold_it)
})
visible(hold_it, TRUE)

“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:

library(gWidgets)
options(guiToolkit="RGtk2")
require(fortunes)                       # just for fun

hold_it <- gbasicdialog(do.buttons=FALSE)
b <- gbutton("click me for a message", cont=hold_it, handler=function(h,...) {
  gmessage(paste(fortune(), collapse="\n"), parent=hold_it)
})
visible(hold_it, TRUE)

The same works for the "tcltk" toolkit. It uses pretty much what Greg suggests can be done.

作业与我同在 2024-12-18 06:13:17

这个主题可能已经结束了,但作为小部件的新手,我曾经遇到过。 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... and w 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... :-)

森罗 2024-12-18 06:13:17

处理此问题的标准方法是请求用户输入以继续。这一句就可以解决问题。

编辑: readline 仅在交互式使用下有效,因此我将其替换为 scan,后者有点不太漂亮。

pause_for_input <- function()
{
  message("Press ENTER to continue")
  invisible(scan(n = 0, quiet = TRUE))
}

所以你的脚本应该看起来像

#Create you GUI
#Whatever else
pause_for_input()

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 for scan, which is a little less pretty.

pause_for_input <- function()
{
  message("Press ENTER to continue")
  invisible(scan(n = 0, quiet = TRUE))
}

So you script should look like

#Create you GUI
#Whatever else
pause_for_input()
℡Ms空城旧梦 2024-12-18 06:13:17

如果您使用 tcltk而不是 gWidgets,那么您可能会使用 tkwait.window 函数code>tcltk 告诉脚本等待 gui 窗口消失后再继续执行脚本。

If you are using the tcltk package instead of gWidgets then you could possibly use the tkwait.window function from tcltk to tell the script to wait until the gui window goes away before continuing the script.

情丝乱 2024-12-18 06:13:17

我发现,一个好方法是使用 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 until gtkMainQuit() is called.

情释 2024-12-18 06:13:17

为了完整起见:ozjimbob 已经给出了最“干净”的方法的答案。
ffeschet 的答案对我不起作用,无论是在 Unix 还是 Windows 上。

因此,在主“启动”脚本中,您至少必须具有以下条目:

options("guiToolkit"="RGtk2")
library(RGtk2)
library(gWidgets)
library(gWidgetsRGtk2)
StartMyGUI()
gtkMain()

在“子”进程“StartMyGUI()”中,您的代码可能如下所示:

StartMyGUI <- function(handler=function(h,...) {
        dispose(h$obj)
    }) {

window <- gwindow("Hello")
group <- ggroup(container = window)
glabel("Hello World!", container=group, expand=TRUE)

# A group to organize the buttons
button.group <- ggroup(container = group)
# Push buttons to right
addSpring(button.group)
gbutton("OK", handler=handler, container=button.group)
gbutton("Cancel", handler = function(h,...) {
            dispose(window)
            gtkMainQuit()
        },
        container=button.group)

return()
} 

仅当用户点击“取消”时将调用 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:

options("guiToolkit"="RGtk2")
library(RGtk2)
library(gWidgets)
library(gWidgetsRGtk2)
StartMyGUI()
gtkMain()

In the "child" process "StartMyGUI()", your code could e.g. look like this:

StartMyGUI <- function(handler=function(h,...) {
        dispose(h$obj)
    }) {

window <- gwindow("Hello")
group <- ggroup(container = window)
glabel("Hello World!", container=group, expand=TRUE)

# A group to organize the buttons
button.group <- ggroup(container = group)
# Push buttons to right
addSpring(button.group)
gbutton("OK", handler=handler, container=button.group)
gbutton("Cancel", handler = function(h,...) {
            dispose(window)
            gtkMainQuit()
        },
        container=button.group)

return()
} 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文