0

I want to write small script with simple GUI using Rscript or littler. In the example I use gWidget2RGtk2.

For example, helloworld.R

#!/usr/bin/r
library(gWidgets2RGtk2)

W <- gwindow("Window", visible=FALSE)
L <- glabel("Hello World!", container=W)

visible(W) <- TRUE

This works well if it run in a R session, but get an error when it run from shell:

Error in UseMethod(".gwindow") : 
no applicable method for '.gwindow' applied to an object of class "NULL"

In the case of graphics, I know that is required X11() before use plot().

Is possible fix this script to allow render widgets from shell?

(I only need run the script on linux machine)


EDIT: This is an example that works well on Linux. (includes suggestions received in the answer and comment.)

#!/usr/bin/r
require(RGtk2) # required for gtkMain()
require(gWidgets2) 
options(guiToolkit="RGtk2")

W <- gwindow("Window", visible=FALSE, 
  handler = function(h, ...) {
    gtkMainQuit() # stop main loop when windows is closed.
  }
)
L <- glabel("Hello Word!", container=W)
visible(W) <- TRUE

gtkMain() # start main loop to keep the script alive.
fnd
  • 335
  • 2
  • 9
  • There are two issues. Dirk's comment is that even if this did work, the script will terminate immediately. To avoid that you can use a modal window, or some other trick. However, the other issue is the failure to find `.gwindow`. I'm not sure -- as I can't really test -- but you might try `require(gWidgets2); options(guiToolkit="RGtk2");` instead of requiring the toolkit package directly. – jverzani Sep 22 '15 at 15:10
  • Thanks! this fix the second issue. Now, I can use some tricks to keep the script running. – fnd Sep 23 '15 at 02:28

1 Answers1

1

Yes, I have done that in the past. You have to make sure you have a GUI event loop running to keep the app alive by waiting.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725