0

I have a r Script with the code:

args = commandArgs(trailingOnly=TRUE)
myData <- read.csv(file=args[0])

I want to run this using a GUI and deliver a choosen csv file with this python code

from tkinter import filedialog
from tkinter import *
import subprocess

window = Tk()
window.geometry('500x200')
window.title("Wordcloud Creator")
lbl = Label(window, text="1. Please prepare a CSV (-Trennzeichen) file with the columns untgscod, berpos, SpezX3")
lbl.grid(column=0, row=0)
def runScript():
    filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("csv files","*.csv"),("all files","*.*")))
    subprocess.call(['Rscript', 'C:/Users/Name/Desktop/R-GUI/test.r', filename])
btn = Button(window, text="Select a file and start Cloud creation", command=runScript())
btn.grid(column=0, row=1)
window.mainloop()

But unfortunately this is not working. I get this error but do not know what is wrong.

  File "c:\Users\name\.vscode\extensions\ms-python.python-2019.2.5558\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 444, in new_CreateProcess
    return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args)
FileNotFoundError: [WinError 2] The system cannot find the file specified

I do not see why the file cannot be found.

ruedi
  • 5,365
  • 15
  • 52
  • 88
  • 1
    Is your path correct? The `FileNotFoundError` informs me that you might have given it a relative path but that is not the path in which the file resides – Mike Tung Mar 27 '19 at 13:18
  • Possible duplicate of [Using a Windows path within Python's subprocess (point to an executable) \[beginner\]](https://stackoverflow.com/questions/41186323/using-a-windows-path-within-pythons-subprocess-point-to-an-executable-beginn) – Bram Vanroy Mar 27 '19 at 13:26
  • I added filename = '"' + filename + '"' and filename.replace("\\", "/") but still get the same error. print(filename) gives me "C:/Users/name/Desktop/file.csv" – ruedi Mar 27 '19 at 13:45
  • Is Python script running in same environment as R? Also, use `os.path.join()` from built-in `os` module for os-agonstic file paths. Check if Python can see path with `os.path.exists()`. Finally, do note, R has its own GUI modules: `gWidgets2`, `RGtk2`, `rattle`! – Parfait Mar 27 '19 at 13:50
  • The environment variable was not set correct. FileNotFoundError is a bit confusing here. Thanks for your help! – ruedi Mar 27 '19 at 16:39

2 Answers2

0

As suggested in comments, check that

  • your paths are correct and do not contain empty spaces or weird characters
  • files do exist in correct location

...and if it doesn´t help, you could try to use subprocess.run instead of subprocess.call.

Oka
  • 1,318
  • 6
  • 11
0

I don't know anything about python, so I can't help you there, but your Rscript is calling the zeroth element of your arguments, which is just an empty character.

R starts indexing at 1.

so if my script was:

args <- commandArgs(trailingOnly = TRUE)
print(args[0])

it would return:

[1] character(0) # this is R telling you that the atomic is a character, but it has zero length

Your RScript should be:

args <- commandArgs(trailingOnly = TRUE)
MyData <- read.csv(file = args[1])

Also, if that's your whole Rscript, 'MyData' is going to disappear as soon as that RScript closes. If you want to create a file in R, you'll need to use:

write.table(<whatever>)

with the appropriate arguments for your data.

Nick
  • 312
  • 1
  • 14