Le blog de pingou - Tag - pygtkLe blog de pingou, ses actualités sur Fedora, ses RPMs, ses tests, son Linux... :-)
Pingou's weblog, his fedora's news, his RPMs, his tests, his Linux... :-)2022-02-17T10:46:15+01:00pingouurn:md5:66db5ce1ed1a80cb2f424695b4bb7780DotclearA simple pygtk R consoleurn:md5:5274aaa899cacaaed5500a17b612d6e62011-01-31T09:27:00+00:002011-06-29T14:16:50+01:00Pierre-YvesGénéralAstucespygtkPythonR<p><img src="https://blog.pingoured.fr/public/source.png" alt="source.png" /></p>
<p>A simple pygtk R console with callback</p>
<p>Une petite interface en pygtk pour R avec affichage des sorties de R dans la fenêtre</p> <p><strong><em>English version</em></strong></p>
<p>Yesterday with the help of <a href="http://fedoraproject.org/wiki/HaikelGuemar">Haikel</a> I have made a small interface for R using pygtk.
The Window was design with <a href="http://glade.gnome.org/">glade</a>, it is a really simple interface:
<a href="https://blog.pingoured.fr/public/GUI.png" title="RGUI"><img src="https://blog.pingoured.fr/public/.GUI_s.jpg" alt="RGUI" style="display:block; margin:0 auto;" title="RGUI, Jan 2011" /></a></p>
<p>User can enter their command in the input field, press the button and the given command
will be run in R.</p>
<p>The interface between R and python is made using the <a href="http://rpy.sourceforge.net/rpy2.html">rpy2</a> library.</p>
<p>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.</p>
<p>For this Haikel pointed me to the rpy2 documentation which allows
<a href="http://rpy.sourceforge.net/rpy2/doc-2.2/html/callbacks.html?highlight=console#write-console">callback</a>
from the R terminal. This way one can redirect the output from R into the gtk window.
So here is the magic:</p>
<pre>
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)
</pre>
<p>The glade file and the full python code are available there:</p>
<ul>
<li><a href="http://project.pingoured.fr/misc/file/22a9d3dc55df/rcmd.ui">glade file</a></li>
<li><a href="http://project.pingoured.fr/misc/file/22a9d3dc55df/rcmd.py">python file</a></li>
</ul>
<p>Results ? See:
<a href="https://blog.pingoured.fr/public/Rcmd.png" title="Rcmd.png"><img src="https://blog.pingoured.fr/public/.Rcmd_s.jpg" alt="Rcmd.png" style="display:block; margin:0 auto;" title="Rcmd.png, Jan 2011" /></a></p>