linuxrgwidgetsrscript

Is possible to use GUI with littler?


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.

Solution

  • 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.