R:构建一个简单的命令行绘图工具/捕获窗口关闭事件

时间:2021-03-09 19:16:16

I am trying to use R within a script that will act as a simple command line plot tool. I.e. user pipes in a csv file and they get a plot. I can get to R fine and get the plot to display through various temp file machinations, but I have hit a roadblock. I cannot figure out how to get R to keep running until the users closes the window.

我试图在一个脚本中使用R作为一个简单的命令行绘图工具。即用户管道在csv文件中,他们得到一个情节。我可以得到R罚款并通过各种临时文件阴谋来显示情节,但我遇到了障碍。在用户关闭窗口之前,我无法弄清楚如何让R继续运行。

If I plot and exit, the plot disappears immediately. If I plot and use some kind of infinite loop, the user cannot close the plot; he must exit by using an interrupt which I don't like. I see there is a getGraphicsEvent function, but it claims that the device is not supported (X11). Anyway, it doesn't appear to actually support an onClose event, only onMouseDown.

如果我绘制并退出,则绘图立即消失。如果我绘制并使用某种无限循环,则用户无法关闭该图;他必须使用我不喜欢的中断退出。我看到有一个getGraphicsEvent函数,但它声称不支持该设备(X11)。无论如何,它似乎实际上不支持onClose事件,只有onMouseDown。

Any ideas on how to solve this?

关于如何解决这个问题的任何想法?

edit: Thanks to Dirk for the advice to check out the tk interface. Here is my test code that works:

编辑:感谢Dirk提供了查看tk界面的建议。这是我的测试代码:

require(tcltk)
library(tkrplot)

## function to display plot, called by tkrplot and embedded in a window
plotIt<-function(){ plot(x=1:10, y=1:10) }
## create top level window
tt<-tktoplevel()
## variable to wait on like a condition variable, to be set by event handler
done <- tclVar(0)
## bind to the window destroy event, set done variable when destroyed
tkbind(tt,"<Destroy>",function() tclvalue(done) <- 1)
## Have tkrplot embed the plot window, then realize it with tkgrid
tkgrid(tkrplot(tt,plotIt))
## wait until done is true
tkwait.variable(done)

1 个解决方案

#1


5  

You need something with a distinct event loop --- and the best portable solution is to rely on the (already included) tcltk package. Start with its demos.

你需要一个具有独特事件循环的东西---最好的便携式解决方案是依赖于(已经包含的)tcltk包。从它的演示开始。

The simplest case may be

最简单的情况可能是

> library(tcltk)
> tk_messageBox(message="Press a key")

which pops a box you need to acknowledge to proceed.

弹出一个框,你需要确认继续。

#1


5  

You need something with a distinct event loop --- and the best portable solution is to rely on the (already included) tcltk package. Start with its demos.

你需要一个具有独特事件循环的东西---最好的便携式解决方案是依赖于(已经包含的)tcltk包。从它的演示开始。

The simplest case may be

最简单的情况可能是

> library(tcltk)
> tk_messageBox(message="Press a key")

which pops a box you need to acknowledge to proceed.

弹出一个框,你需要确认继续。