3

I made a GUI using RGtk2 named Main.R that works perfectly when run from RStudio but only works in CMD when I open R in it and then source the script. My goal is to make it fully portable and thus to write a .bat file able to run it.

Classical commands R CMD BATCH Main.R and Rscript Main.R don't work. In these cases, instead of waiting for me to interact, the window is closed immediately after its creation. After some research I only found out that the OP from run R script from .bat (batch file) (sadly unanswered) encountered a similar issue, but my question differs from his so I still believe my post is justified.

These commands don't even work for a very simplistic program I made with the same library, which indicates that the issue may be caused by GTk itself.

library(RGtk2)

Destroy <- function(button) {
  window $ destroy()
}

vbox <- gtkVBoxNew (FALSE, 10, TRUE) #container for the first tab
vbox $ setBorderWidth (10)

label <- gtkLabelNew ("Welcome ! Click on the button to close the window")
vbox $ packStart (label, TRUE, 20, 0)

closeButton <- gtkButtonNewWithLabel("Close")
vbox $ packStart(closeButton, FALSE, FALSE, 0)
gSignalConnect (closeButton , "clicked" , Destroy)
#links the startButton to the Start function

window <- gtkWindow (NULL, FALSE)
window ["title"] <- "Example"
gtkWindowResize(window, 800, 450)
window $ add (notebook)

window $ showAll()

In my batch file, I tried this among other variants :

@echo off
title OCR GUI
start R.exe & "source("OCR_MR/R/Main.R")"

But of course the file executes the last line when the R session is closed manually. As R.exe can't take any arguments I need to make my batch file make an input inside R. Is there any way to do it ?

Any help will be highly appreciated !

UPDATE : Thanks to some comments I realised Rscript doesn't wait for any kind of input, even

readline(prompt="message")

doesn't work. It may be an issue of running interactive commands in a non-interactive mode, will keep you updated !

  • Does adding `readline(prompt = "Hit Enter to quit: ")` to the end of the script fix the issue? – Dason Jun 22 '18 at 15:04
  • 3
    Please consider `Rscript` which has now existed for over a decade and is better than `R CMD BATCH` in every dimension. On Unix, `r` from littler is also good (though I am the (co-)author and hence not free of bias). – Dirk Eddelbuettel Jun 22 '18 at 15:05
  • Thanks for your comments ! Sadly I can't make Rscript display the window correctly even with @Dason's line. I start to believe it may be an software installation issue as it doesn't wait for an input even when I just ask it to wait for an input via the readline command, will keep you updated – Maxime Roullet Jun 25 '18 at 07:30

1 Answers1

3

Found the answer : I tried to use R interactively via batch mode, which obviously couldn't work as batch is non-interactive.

I nevertheless found a workaround thanks to this answer : https://stackoverflow.com/a/11567220/9765404, consisting in downloading R-Portable with all libraries used by my project installed locally and adding this function to the Rprofile.site file (in App/R-Portable/etc) :

.First = function(){ #.First() is the first function R-Portable will execute when opened
    .libPaths(.Library) #force R-Portable to use the local library
     source("path-to-file") #launches the program
}

Then you can just export the folder containing R-Portable and your project to any Windows computer and execute R-Portable.exe, it works without having R installed.