我有一个R脚本,它查询数据库,运行一些分析,并根据当前系统日期绘制几个图表。我希望每天在启动时运行此脚本,我认为可以使用rscript.exe的快捷方式和必要参数来实现这一点。这很好用,但是脚本在运行后就退出了,对于查看图表来说并不太有用。我正在使用XP和win7。有没有一种简单的方法可以保持脚本的输出屏幕上?我尝试将scan合并到脚本中,但它不会暂停。我知道我可以打开rgui,并运行一行代码,但计划将其部署到完全不熟悉R的同事的计算机上。
这个在Linux系统上对我有用:
#!/usr/bin/env Rscript
X11()
with(mtcars, plot(mpg, hp))
locator(1)
windows()
调用也可以实现此功能。Michael的解决方案可能已经可行了,但是这里有一些内容可以在tkrplot框架中显示绘图。 tkrplot包(在CRAN上)使用tcltk扩展到R,并且可以在任何地方使用。
# From https://dev59.com/VE7Sa4cB1Zd3GeqP4IAp
# r-building-a-simple-command-line-plotting-tool-
# capturing-window-close-events
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) }
tt <- tktoplevel() ## create top level window event handler
done <- tclVar(0) ## variable to wait on
## 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))
tkwait.variable(done) ## wait until done is true
## script continues, or exits, ... once plot is closed
如果您查看R的tcltk文档,您会发现其他带有“确定”按钮等的示例。
好的,我本来打算完全抱怨一下我看到的所有类似问题的答案,因为它们都无法在Windows上运行。readline、tkwait.window、Sys.sleep(1e30)、while(TRUE),这些都没有用。
但是我刚刚将R更新到v3.1.0,现在tkwait.window(yourmainwindow)可以工作了,while(TRUE){}也可以工作了,尽管Sys.sleep(1e30)仍然不起作用。
没关系...我正在使用tkwait.window,因为它是tk,可以等待我的窗口(这正是我想要的)。
使http://www.sciviews.org/_rgui/tcltk/OKtoplevel.html中的示例代码工作...(为简洁起见删除了注释)
require(tcltk)
tt <- tktoplevel()
OK.but <- tkbutton(tt, text = "OK", command = function() tkdestroy(tt))
tkgrid(OK.but)
tkfocus(tt)
tkwait.window(tt) # <-- added this to make the window stay!
brew
包感兴趣,比 LaTeX 更简单)? - Ben Bolker