1

I've recently started programming in python for my job, so I'm quite new to it. My goal is to create a graphic interface so that the user can run a program that I have been developing in R. The interface is done using the Tkinter module from python (version 3.3).

The problem comes when I have to call the R interpreter from python to run an R file that is generated (run.R file). The curious thing is that this only happens when I try to run my script in Windows, not in Linux. In both cases, I am trying to use the os module from python.

This is the code that is not working for Windows:

os.chdir(outRW) #first I change the working directory to the one where the run.R file is

os.system("C:\R-3.6.1\bin\Rscript run.R")

When I execute this, it changes the directory successfully, but when it comes to calling the R interpreter, it shows me this error:

The filename, directory name, or volume label syntax is incorrect.

However, I have tried running the "C:\R-3.6.1\bin\Rscript run.R" command in the Windows Command Prompt and it works perfectly. I have also tried adding the path to R to the environmental variables, but again I could only make it work in the Command Prompt, not with python.

I guess there is something very obvious that I am missing here, but I cannot see it.

Any help or comments are very much appreciated!

Thank you!

2
  • Try os.system("C:/R-3.6.1/bin/Rscript run.R"). \r is the escape sequence for a carriage return and by using forward slashes you avoid any escaping. I would also suggest using subprocess instead of os.system. Commented Nov 25, 2019 at 14:56
  • Thank you very much! This also solved the problem! Commented Nov 25, 2019 at 15:21

1 Answer 1

1

Use double backslashes.

In R you need to use double backslashes \\, otherwise it'll try to interpret it as an Escape Character.

Use this and it will work:

os.system("C:\\R-3.6.1\\bin\\Rscript run.R")

Hope this helps.

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

2 Comments

Thank you very much! it did work for me! Now I have a different problem since R tells me that it can't find a package that this code needs, however it is installed and when I run the same command in the prompt it works. Why is this happening now?
You're welcome, happy it worked! I think this other problem is unrelated to the first one. Open a new question and we'll gladly help you! :)

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.