我想使用 Rscript
或 littler
编写带有简单 GUI 的小脚本。
在示例中,我使用了 gWidget2RGtk2
。
例如,helloworld.R
#!/usr/bin/r
library(gWidgets2RGtk2)
W <- gwindow("Window", visible=FALSE)
L <- glabel("Hello World!", container=W)
visible(W) <- TRUE
如果它在 R session 中运行时效果很好,但从 shell 运行时会出错:
Error in UseMethod(".gwindow") :
no applicable method for '.gwindow' applied to an object of class "NULL"
对于图形,我知道在使用 plot()
之前需要 X11()
。
是否可以修复此脚本以允许从 shell 渲染小部件?
(我只需要在linux机器上运行脚本)
编辑:这是一个在 Linux 上运行良好的示例。 (包括在回答和评论中收到的建议。)
#!/usr/bin/r
require(RGtk2) # required for gtkMain()
require(gWidgets2)
options(guiToolkit="RGtk2")
W <- gwindow("Window", visible=FALSE,
handler = function(h, ...) {
gtkMainQuit() # stop main loop when windows is closed.
}
)
L <- glabel("Hello Word!", container=W)
visible(W) <- TRUE
gtkMain() # start main loop to keep the script alive.
最佳答案
是的,我过去曾这样做过。您必须确保有一个 GUI 事件循环正在运行,以通过等待使应用程序保持事件状态。
关于linux - 是否可以将 GUI 与 littler 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706253/