-2

I wrote an R App (with gWidgets) and it works fine in RStudio.

However, when I created bat file, it loads code just fine and it actually opens the first window of the app, but then the app closes and no error is thrown.

My batch file is simply:

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

Regarding my R code it is 99% of functions, however my last thing is not a function, but a code to open the welcome window (simplyfied):

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)

What do I have to do?

Paxz
  • 2,959
  • 1
  • 20
  • 34
Vesnič
  • 365
  • 5
  • 17
  • 4
    Hey, please post the code of the batch-flile and and the R App and try to achieve a [mcve] so we can try to help you. – Paxz Aug 27 '18 at 11:52
  • I've edited my post regarding batch-file. Regarding R code it is too long (2600 lines) to poste it all... However I assume it has something to do with the fact, that the program runs until the end and it simply closes, as nothing stops it... But I don't know how to prevent that. – Vesnič Aug 27 '18 at 12:19
  • related https://stackoverflow.com/questions/18730175/gwidgets-gui-cannot-display-when-called-with-r-cmd-batch – Stéphane Laurent Aug 27 '18 at 12:24
  • Also look at https://www.r-bloggers.com/r-creating-a-shortcut-to-run-a-gwidgets-gui/ and https://stackoverflow.com/questions/10312417/running-an-r-script-using-a-windows-shortcut/10320598 – Stéphane Laurent Aug 27 '18 at 12:27
  • Try also this code `.First <- function(){ source('yourapp.R') }` and save it as a Rdata file in the folder of your app. Sorry I'm only vaguely remember how I did in the past.... – Stéphane Laurent Aug 27 '18 at 12:31

1 Answers1

1

I recommend you to add gtkMain() at the end of script, it will cycle until destroy message would be sent. Please see as below:

gtk.R source file:

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:

Output

Artem
  • 3,304
  • 3
  • 18
  • 41