A simple pygtk R console
By Pierre-Yves on Monday, January 31 2011, 09:27 - Général - Permalink

A simple pygtk R console with callback
Une petite interface en pygtk pour R avec affichage des sorties de R dans la fenêtre
English version
Yesterday with the help of Haikel I have made a small interface for R using pygtk.
The Window was design with glade, it is a really simple interface:

User can enter their command in the input field, press the button and the given command will be run in R.
The interface between R and python is made using the rpy2 library.
The tricky part was to actually output the results from R directly in the gtk window as their are generated, in other word, showing the output of the function before the end of the function as some R commande can take a little while.
For this Haikel pointed me to the rpy2 documentation which allows callback from the R terminal. This way one can redirect the output from R into the gtk window. So here is the magic:
def callbackFunction(self, x):
""" Function which redirect the output from R to the gtk window """
if self.gtkbuffer is None:
print x
else:
self.gtkbuffer.insert(self.gtkbuffer.get_end_iter(), x)
while gtk.events_pending():
gtk.main_iteration()
def runR(self, cmd):
"""
Function runs in R the given command and set the redirection
of the output
"""
if len(cmd) > 0:
self.addToText("> %s" %cmd)
rinterface.set_writeconsole(self.callbackFunction)
robjects.r("%s" %cmd )
# restore default function
rinterface.set_writeconsole(rinterface.consolePrint)
The glade file and the full python code are available there:
