R:在cmd中运行R App时,它会加载并关闭

时间:2018-08-27 11:45:45

标签: r batch-file cmd gwidgets

我编写了一个带有gWidgets的R App,它在RStudio中可以正常工作。

但是,当我创建bat文件时,它可以很好地加载代码,并且实际上会打开应用程序的第一个窗口,但是随后该应用程序关闭并且未引发任何错误。

我的批处理文件很简单:

<path where R is installed> <path where my program is saved>

关于我的R代码,它是99%的函数,但是我的最后一件事不是函数,而是打开欢迎窗口的代码(简化):

First_window <- gwindow("Welcome") 
g <- ggroup(horizontal = FALSE, container = First_window)
gtext("Welcome to Recovery Plan application", container = g, expand=TRUE)
gtext("Do you want to start a new project or open an old one?", container = g)
gbutton("New project", container=g, handler=function(h,...) foo_function)

我该怎么办?

1 个答案:

答案 0 :(得分:1)

我建议您在脚本末尾添加gtkMain(),它将循环执行直到发送销毁消息为止。 请看如下:

gtk.R源文件:

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

First_window <- gwindow("Welcome") 
g <- ggroup(
  horizontal = FALSE, 
  container = First_window)

gtext(
  text = "Welcome to Recovery Plan application", 
  container = g, 
  expand=TRUE)

gtext(
  text = "Do you want to start a new project or open an old one?", 
  container = g)

gbutton(
  text = "New project", 
  container = g,
  handler = function(h,...) gtkMainQuit)

gtkMain()

run.bat

 @echo off
"<path to R bin> \R.exe" CMD BATCH --no-save --no-restore "<path to R-file>\gtk.R" 

输出:

Output