R:当 运行 R App 在 cmd 中加载并关闭

R: when running R App in cmd it loads and closes

我写了一个 R 应用程序(带有 gWidgets),它在 RStudio 中运行良好。

但是,当我创建 bat 文件时,它会很好地加载代码并且它实际上会打开应用程序的第一个 window,但随后应用程序会关闭并且不会抛出任何错误。

我的批处理文件很简单:

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

关于我的 R 代码,它是 99% 的函数,但是我最后做的不是函数,而是打开欢迎的代码 window(简单):

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)

我需要做什么?

我建议你在脚本末尾添加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" 

输出: