2

I'm currently working on a GUI for a R script I have written earlier. Since I have some experience in Python I decided to use tkinter for that.

The GUI should mainly consist of a few entry-options and a "start analysis" button. So far no problems.

I have written a function called call_r() which starts the R script using subprocess.run(...), the arguments will later come frome the users entries:

    def call_r():
        r_script = 'C:/Program Files/R/R-4.0.5/bin/Rscript.exe'
        path_to_file = 'C:/.../Automated Analysis'
        r_file = path_to_file + '/data_analysis.R'
        args = ['C:/.../test/files', 'C:/.../test/results',
                '2', '0.5', '3.5', '3', '5']

        call = [r_script, r_file] + args

        subprocess.run(call, shell=True)

If the function is called by itself it works completely fine (the .R script is executed and does its job), but as soon as I connect the function to a TkButton the R script opens, but can't import the libraries:

Error in library(ggplot2) : there is no packages called 'ggplot2'

I guess R does not find the libraries anymore...

This is the very basic tkinter GUI (just for testing reasons):

    root = tk.Tk()
    root.title("Automated Data Analysis")
    root.geometry("50x50")
    
    button = tk.Button(master=root, text="Run Analysis", command=call_r)
    button.pack()
    
    root.mainloop()

The problem does not occur if call_r() is executed without the tkinter GUI. Can someone explain what is going on and how to fix this?

Update: After trying a few different things I was able to narrow down the problem. As soon as a tkinter function is called in the Python script (e.g. root = tk.Tk() ), the .libPaths() function in R which returns the directory of the installed packages does not work properly anymore. It returns just one, instead of two directories.

Hope that is somehow useful information...

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
Mr_Zebi_97
  • 21
  • 2

0 Answers0