0

I want to call an R program from a python script.

I wrote the following:

os.system("cat " + variableName + " | code.R")

It returns the error: sh: 1 : code.R: not found cat: write error: Broken pipe

Yet, I am sure of the name of the R file.

Why is it not working?

6
  • I don´t know about *.R programs. But... is code.R a binary file or a script that has to be interpreted? Commented Mar 11, 2014 at 17:49
  • @RaydelMiranda A script that has to be interpreted Commented Mar 11, 2014 at 17:49
  • Is the code.R file in the same directory where from you are calling the python script? Commented Mar 11, 2014 at 17:50
  • What's in variableName? Commented Mar 11, 2014 at 17:56
  • 1
    Maybe this can help: stackoverflow.com/questions/4106565/… Commented Mar 11, 2014 at 18:08

2 Answers 2

1

So, if code.R is a script that has to be interpreted you must build the pipe to the interpreter not to the script. You receive a Broken PIPE error because code.R by it self don't know how to handle command line arguments.

On the other hand if what you want is store the variable value inside code.R you have to change | by >>.

os.system("cat " + variablename + ">> code.R")

EDIT: Since it's working from terminal, try this:

import subprocess
input = open(variableName, "r")
result = suprocess.call(["code.R"], stdin=input)    # result is the return code for the command being called.

see subprocess.call for more details.

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

2 Comments

Actually, code.R takes variable as input. What bothers me is that, from the terminal, cat variable | code.R worked perfectly. I would like to call this command from my python script, but for some reason it doesn't work...
the problem is then solved. Thank you guys for your help
1

Is code.R in the current working directory? Is it executable? Can you run cat xxx | code.R from the shell and have it work properly, instead of running your python program?

2 Comments

Yes to all these questions (for the executable part, ls -lrt gives -rwxr-xr-x. I had executed the code.R from another directory before copying it to the one where my python script is located)
Do print os.getcwd() immediately before the os.system() call?

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.