J R
J R

Reputation: 181

Interacting with R script via Python subprocess

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.

Upvotes: 0

Views: 532

Answers (2)

lcgong
lcgong

Reputation: 471

Try PypeR ?

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

Upvotes: 2

User
User

Reputation: 14863

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?

Upvotes: 1

Related Questions