1

By google I have found that the way to call R code is to use the following:

subprocess.call("Rscript" + " /path/Rscript.R")

# The reason there is an add statment is because in actual code I have a 
variable of where the script is. 

On my home computer such code works. I am now working on a Server machine. If while in the the direcotry of my code I run

Rscript /path/Rscript.R

it works. However when I try to run it from the python code I get No such file or Directory. I have made sure Rscript was in my path (because I could Run it from command line).

Any help would be appreciated.

I have tried running from ~/path to it, ./path to it, /absolutepathtoit.

1 Answer 1

2

There's a needless parenthesis at the end of the line:

subprocess.call("Rscript" + "/path/Rscript.R"))
                                              )

And, you need to insert a space between a command and arguments. Otherwise Rscript/path/Rscript.R is recognized as a command.

subprocess.call("Rscript" + " " + "/path/Rscript.R")
                            ^^^

Or pass a list:

subprocess.call(["Rscript", "/path/Rscript.R"])
Sign up to request clarification or add additional context in comments.

4 Comments

The parenthesis and lack of space comes from my sloppy editing to make the path smaller. It is there in the acutal code. I am going to try to use the list method.
@user2100799, If passing a list also does not work, try it with shell=True argument.
The list worked! Although I read on this site that using shell=True was advised against. I can only accept after waiting for 5 mins.
Glad you got the problem solved. Depending on exactly how the Python+R is getting executed, you should give rpy2 a go since--in certain circumstances--it could be much more efficient to use R code that way.

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.