RJython? [was Re: [R-gui] [RGUI] Where do you start?]

david.whiting at ncl.ac.uk david.whiting at ncl.ac.uk
Fri Feb 13 10:59:46 MET 2004


I'm more than a little late joining this thread, but ...

On Mon, Jan 19, 2004 at 12:32:01PM -0800, Duncan Lang wrote:

[...]

> The tcltk package was put into the R distribution as a matter of
> convenience rather than a formal decision to advocate or support the
> use of Tcl/Tk as the main toolkit.  This bundling has given it a
> status that was never discussed.  Iit does run on all of the primary
> platforms which is very helpful.  However, it is an old toolkit that
> is not used for any large application.  This is in contrast with Gtk,
> Qt, Swing, which have richer widgets, more activity in development and
> are the bases for numerous applications. Why the latter is important
> is that we have been succesful in embedding R into other applications
> and using and extending their GUIs.  

[...]

First some caveats: I have dabbled with Java (and felt totally
intimidated by the code required to make a GUI using swing), and have
dabbled even less with Python, am aware of RSpython and SJava, but know
very little about their internal workings.

I have, however, recently had need to quickly create a GUI that
integrates with an application written in Java. I found Jython, a Java
implementation of Python.  This combines the scripting language
capabilities of Python with the rich libraries of Java.  There are some
neat tricks built into Jython that take away much of what intimidated me
when looking at using swing directly.  Jython is a Java *implementation*
of Python, so it *is* Python (the 'normal' python can be considered
Cpython).  This means that Jython can run python scripts.  What Jython
cannot do is use modules written in C - they would need to be migrated
to Java.

I wonder if some combination of SJava and RSpython code, probably
implemented in Java,  could provide a library that gives R users access
to swing, via jython, from R.  

I note that the FAQ says "...for technical reasons, currently the best
way to integrate Java and R is to run Java and have it load R"  in which
case it might be easier to think about using R from within Jython.  I
remember that someone posted a list of similarities between R and Python
to the main R-help mailing list and during my quick use of Jython I have
seen that the 'feel' is certainly very similar.  I think R-users should
be able to make useful things happen with Jython without having to go
though too much of learning curve. 

To give you a feel of what Jython code for creating a GUI looks like I
have a very simple example below:


## File: simpleJythonGui.py

## Load the Swing library (the Java GUI toolkit) into Jython 
## This is like using library() in R.

import javax.swing as swing


## Create a function.  This will be called when the button is 
## pressed (see below).  All it does is import another library 
## and make the application exit.  The parameter passed to the 
## function is the event that takes place when the button 
## is pressed.

def do_quit(event=None):
    import sys
    sys.exit(0)


## Create a window ('frame' in Java-speak) and specify its size.  
## In Java this would require several lines of code and I don't 
## think that the size can be specified as simply as it can in
## Jython.  The title is set by the first parameter.

f = swing.JFrame("This is the main window", size = (400, 200))


## Now create a panel.  Panels go inside frames/windows.  There
## can be more than one panel in a frame and the positions of the
## panels can be controlled by various layout managers.  In this simple
## example I will not use a layout manager (at least not explicitly - I 
## don't know if  there is some  implicit use of a layout manager going 
## behind the scenes).

p = swing.JPanel()


## Now I want to create a button.  For Java programmers: Jython does 
## some very neat things with Java beans and converts methods like 
## setSomeProperty() and getSomeProperty() to attributes that can 
## be assigned to, named someProperty (note the change of case of 
## the first letter).  This greatly simplifies the use of event 
## listeners (and other things).  
## When the button is pressed, the function do_quit will be called. 

b = swing.JButton("Press me to exit", actionPerformed=do_quit)


## Now add the button to the panel

p.add(b)


## Now add the panel to the frame/window.
f.contentPane.add(p)


## And finally show the window 
f.show()

### End of File: simpleJythonGui.py


So, 10 lines of code, including the import statement and the function,
to create a GUI with a button that performs an action when the button is
pressed. 

I mostly hack around with scripts to get my work done and don't think
that I have the experience or training to be able to implement something
that might be 'Rjython', so all I can do is offer this idea without
being able to offer to attempt to implement it.  At least, not yet.
Perhaps others on list have more experience of Java and R to know whether
this could work.

Dave

-- 
Dave Whiting
Dar es Salaam, Tanzania



More information about the R-SIG-GUI mailing list