0

I was wondering how to open an R script and interact with it (for example, send strings, integers etc.). Although I have not used it before, subprocess seemed like a reasonable way to do this.

So far, I have

process = subprocess.Popen(['/path/to/Rscript --no-save path/to/script.R'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)

This appears to be successful in opening script.R, however in my script I prompt the user to enter an integer and I cannot quite sort out how to do this. I have tried:

process.communicate(input=1)[0]

But I appear to be barking up the wrong tree. The subprocess closes without appearing to receive this input.

EDIT: Rpy is probably not a good alternative at this point, because users of this script will not necessarily have access to that module and its dependencies.

2
  • what is the result of process.communicate(input=1)? Commented Apr 2, 2013 at 14:30
  • Right - good question, I should have specified that I see no visible output as a result of process.communicate(input=1) Commented Apr 2, 2013 at 14:39

2 Answers 2

2

Try PypeR ?

It is an great interface to use R in python through pipe.

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

1 Comment

This was really useful, thanks. I was able to send commands through to my scripts using this approach.
1

EDIT2

What about that?

process = subprocess.Popen(['/path/to/Rscript', '--no-save', 'path/to/script.R'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)

EDIT1

You can only comunicate strings between processes over stdin and stdout.

Does process.communicate("1\n") help you out?

2 Comments

Unfortunately, no. The string vs int distinction does not matter, because the process closes before the input can be passed.
Yes, that seems to help. I can now pipe strings to the subprocess via process.stdin.write('source("script.R")') That's a step forward, thanks!

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.