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?

9
  • Hard to help you without an example? and why not to do this in R ? Commented Mar 7, 2014 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. Commented Mar 7, 2014 at 17:51
  • Please add an example of your script and the variable yo want to change. Commented Mar 7, 2014 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? Commented Mar 7, 2014 at 18:26
  • Yes that is correct. I'm not sure what you mean with Rscript though. Commented Mar 7, 2014 at 18:29

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

How would I implement Rscript within my Python application though? Don't I have to use command line with Rscript?
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.
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.
I see. Then you will have to search how to modify R environmet from Rpy2 rather than to rewrite the script.
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.