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

发布于 2024-09-06 05:07:35 字数 946 浏览 3 评论 0原文

我正在尝试在脚本中使用 R,该脚本将充当简单的命令行绘图工具。即用户通过管道输入 csv 文件并得到一个绘图。我可以很好地使用 R 并通过各种临时文件机制来显示绘图,但我遇到了障碍。我不知道如何让 R 继续运行,直到用户关闭窗口。

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

关于如何解决这个问题有什么想法吗?

编辑:感谢 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)

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.

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.

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:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

谜泪 2024-09-13 05:07:35

您需要具有独特事件循环的东西 --- 最好的可移植解决方案是依赖(已包含的)tcltk 包。从它的演示开始。

最简单的情况可能是

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

弹出一个框,您需要确认才能继续。

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.

The simplest case may be

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

which pops a box you need to acknowledge to proceed.

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