I am building a toolbox for bioinformatical analysis of DNA sequences. All the statistical analysis is done in R. I have written the R scripts for these.
I am now building a GUI with Tkinter so users can choose a file with DNA information and run my R scripts on them. I am at the very beginning of the Tkinter script and I am stuck on getting the information (directory path) the user selects into my R script, which is then executed on that file.
Here is the test code if you want to run the GUI yourself:
import Tkinter
import tkMessageBox
import tkFileDialog
import os
import sys
import subprocess
top = Tkinter.Tk()
CDR3Merge = "PHB_forR.R"
#####----------Definitions------------####
#Definition to open file browser and select file.
def FileOpen():
var.set(tkFileDialog.askopenfilename(defaultextension=".txt"))
#Definition to start R-Script
def CDR3Extract():
os.popen("R CMD BATCH PHB_forR.R").read()
####----------Buttons and their Functions-------------####
#Button to browse and open/select file
ButtonOpenFile = Tkinter.Button(top, text="Open File", command = FileOpen)
ButtonOpenFile.grid(row=1, column=1)
#Button to Execute R-Script
ButtonCDR3Extract = Tkinter.Button(top, text="CDR3 Extraction", command = CDR3Extract)
ButtonCDR3Extract.grid(row=2,column=0)
#####----------Entry Bars and their Definition--------####
#Label at the very top
TopLabel = Tkinter.Label(text="CDR3 Extraction")
TopLabel.grid(row=0, column=0)
#Entry for file selection
var = Tkinter.StringVar()
FileEntry = Tkinter.Entry(top, width=60, textvariable= var)
FileEntry.grid(row=1,column=0)
####---------------------------------------------------####
top.mainloop()
And Im guessing we need to focus on this part:
#Definition to open file browser and select file.
def FileOpen():
var.set(tkFileDialog.askopenfilename(defaultextension=".txt"))
#Definition to start R-Script
def CDR3Extract():
os.popen("R CMD BATCH PHB_forR.R").read()
I don´t know if it is necessary to post my R script, but the method/code with which I read my .txt file in R is following:
read.delim("file.txt",header=TRUE, sep="\t")
How can I turn the output of the askopenfile command from Tkinter into a variable used in my R script? Is this even possible or should I consider a different method of doing this?
Thank you.
Edit: After Dason recommended to check out commandArgs() in R I wanted to print the filepath together with the command to run the Rscript. So now the button CDR3 Extract will do folliwng:
def CDR3Extract():
os.popen("Rscript PHB_forR.R" + " " + str(var).read()
var still being
var = Tkinter.SrtingVar()
The problem I am having now is that var is returned as PY_VAR0 instead of the filepath that is printed in my entry box.
I have tried var.get() and using that but it also returns as PY_VAR0.
Any suggestions? I am probably looking at this the wrong way I can´t find or come up with something more simple.
help(commandArgs)commandArgs()so when I executeRscript PHB_forR.R filename.Rit will use filename.R as the file to read. However, this does not seem to work with the file in a different directory as the script and neither have I been able to get the path of the file into the definition/command in my python script. I thoughtos.popen("R CMD BATCH PHB_forR.R", var).read()would do it, as var is the path directory, however I get a error message that says TypeError: must be string, not instance"