1

I want to read in an R script file into Python (using Tkinter GUI package) and change some of the variables (or just print and play around with them), then re-save those variables back into the R script file. I am taking a look at the Rpy2 module, but I don't see anything in there that will help me accomplish that. The variables that I want to change are string and numeric variables (in R).

For example:

R Script contains:

eventtime<-"18:30:00"   
eventdate<-"2014-02-28" 

Python file:

import Tkinter as tk
from rpy2.robjects import r

class GUI(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master, width=300, height=200)
        self.master = master
        self.master.title('GUI')

        self.pack_propagate(0)
        self.pack()

        self.run_button = tk.Button(self, text='Run', command=self.evaluate)
        self.run_button.pack(fill=tk.X, side=tk.BOTTOM)

        self.entrybox_frame = tk.Frame(self)
        self.entrybox_frame.pack(anchor=tk.S, pady=5)

        self.eventtime_var = tk.StringVar()
        self.eventtime = tk.Entry(self.entrybox_frame, textvariable=self.eventtime_var)
        self.eventdate_var = tk.StringVar()
        self.eventdate = tk.Entry(self.entrybox_frame, textvariable=self.eventdate_var)

        self.eventtime.grid(row=0, column=1)
        self.eventdate.grid(row=1, column=1)

    def evaluate(self):
        # Clicking the Run button will save the variables to the R script
        r.source('file.r')
        self.get_event_info()

    def run(self):
        self.mainloop()

    def get_event_info(self):
        # Get the user input and write them to the R variables
        # So first must read the R script into python, then rewrite over those variables 
        # Then save the R script
        print self.eventtime_var.get()
        print self.eventdate_var.get()

gui = GUI(tk.Tk())
gui.run()

Any ideas?

KidSudi
  • 460
  • 2
  • 7
  • 19
  • Hard to help you without an example? and why not to do this in R ? – agstudy Mar 07 '14 at 17:36
  • So essentially I'm creating a GUI using Tkinter, where the user inputs some variables, and I want to save those variables to the R script, then call rpy2.robjects.r.source('filename') within Python, which performs a whole another set of operations. – KidSudi Mar 07 '14 at 17:51
  • Please add an example of your script and the variable yo want to change. – agstudy Mar 07 '14 at 18:04
  • It seems to me that purpose of your rewriting is to to tweak variables and run the R script again, is that right? Why not to use Rscript interpreted script with positional parameters as your variables? – Petr Matousu Mar 07 '14 at 18:26
  • Yes that is correct. I'm not sure what you mean with Rscript though. – KidSudi Mar 07 '14 at 18:29
  • I mean that if the purpose of your doing is to enable running rscript with another set of of eventtime and eventdate, you do not need to rewrite the script. You just need to prepare rscript the way that it could be run directly as command with eventtime and eventdate as positional parametres. Is your system unix like? – Petr Matousu Mar 07 '14 at 18:36
  • No it's Windows. The purpose of rewriting the script though is to then use Rpy2 and source the R script file, which then sources another R script file. – KidSudi Mar 07 '14 at 18:37
  • I will supply some example. – Petr Matousu Mar 07 '14 at 18:37
  • There is Rscript.exe which can run your rscript file and allows you to add some positional parametres which are passed into R environment. So you can have just one script. I am not sure if this help you if you need to use Rpy2 as well. – Petr Matousu Mar 07 '14 at 18:43

2 Answers2

2

Rather than hardcoding variables into script is better to use passing them as possitional arguments in Rscript or setting them in R environment before the script (which needs them) is sourced.

Interpreting R scripts with positional erguments

Regarding passing arguments (as your variables) to script, you can find some already answered questions on SO. The above link is just a starter.

R-intro B.4 Scripting with R is the official source.

Rpy2 Changing objects in R environment

You could set or change (by Rpy2 means) variables in R environment before sourcing the rscript, which will use already set variables, so the script must be prepared not to set them but just use them.

Community
  • 1
  • 1
Petr Matousu
  • 3,120
  • 1
  • 20
  • 32
  • How would I implement Rscript within my Python application though? Don't I have to use command line with Rscript? – KidSudi Mar 07 '14 at 19:07
  • Rscript is interpreter, it is usually started from terminal. Why do you need python? To provide interface for the user? I believe that all you need can be don directly from R. – Petr Matousu Mar 07 '14 at 19:09
  • So I don't want to start a terminal, I want everything self-contained within my Python application, which is why I was looking into Rpy2. – KidSudi Mar 07 '14 at 19:11
  • I see. Then you will have to search how to modify R environmet from Rpy2 rather than to rewrite the script. – Petr Matousu Mar 07 '14 at 19:18
  • Thanks! Yes I just assigned 'R variables' within Python, then sourced the R script. It uses those variables in the R environment as you said. – KidSudi Mar 07 '14 at 20:14
0

rpy2 offers a possible better way to handle an R script by encapsulating it into in namespace on the Python end, and in an environment on the R end.

Check the relevant section of the rpy2 documentation.

lgautier
  • 11,363
  • 29
  • 42