gtnbz2nyt
gtnbz2nyt

Reputation: 1535

Display R plots running from Python

I have the following R script called Test.R:

x <- c(1,2,3,4,5,6,7,8,9,10)
y <- c(2,4,6,8,10,12,14,16,18,20)
plot(x,y, type="o")
x
y

I am running it via Python using this Python script called Test.py:

import subprocess

proc = subprocess.Popen(['Path/To/Rscript.exe',
                         'Path/To/Test.R'],
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
print stdout

# Alternative Code to see output
# retcode = subprocess.call(['Path/To/Rscript.exe',
#                            'Path/To/Test.R'])

When I run the python script Test.py, I get the following output in Pycharm:

 [1]  1  2  3  4  5  6  7  8  9 10
 [1]  2  4  6  8 10 12 14 16 18 20

So the usual text results show up fine, but how do I get the plots to show? I've tried changing the file from Rscript.txt to Rgui.exe but I get the following error and it only opens up Rgui:

ARGUMENT Path/To/Test.R __ignored__

is there an easy way for the output to display? I know this is a simple problem but I'm wondering how this will extend to other plot commands in R like acf() or pacf(). Should I use ggplot2 to save he plots and just tell Python to open the saved files?

Thanks.

Upvotes: 1

Views: 933

Answers (1)

Irshad Bhat
Irshad Bhat

Reputation: 8709

Add:

show()

after:

plot(x,y, type="o")

Upvotes: 1

Related Questions