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.

5
  • 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 Commented Mar 27, 2019 at 13:18
  • Possible duplicate of Using a Windows path within Python's subprocess (point to an executable) [beginner] Commented Mar 27, 2019 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" Commented Mar 27, 2019 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! Commented Mar 27, 2019 at 13:50
  • The environment variable was not set correct. FileNotFoundError is a bit confusing here. Thanks for your help! Commented Mar 27, 2019 at 16:39

2 Answers 2

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.

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

Comments

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.

1 Comment

Thany you. I would have been run into this without your help.

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.