【问题标题】:Display R plots running from Python显示从 Python 运行的 R 绘图
【发布时间】:2015-01-31 04:06:24
【问题描述】:

我有以下 R 脚本,名为 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

我通过 Python 使用这个名为 Test.py 的 Python 脚本运行它:

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'])

当我运行 python 脚本 Test.py 时,我在 Pycharm 中得到以下输出:

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

所以通常的文本结果显示得很好,但我如何让图表显示?我尝试将文件从 Rscript.txt 更改为 Rgui.exe 但我收到以下错误,它只打开 Rgui:

ARGUMENT Path/To/Test.R __ignored__

有没有一种简单的方法来显示输出?我知道这是一个简单的问题,但我想知道这将如何扩展到R 中的其他绘图命令,例如acf()pacf()。我应该使用ggplot2 来保存他的绘图并告诉Python 打开保存的文件吗?

谢谢。

【问题讨论】:

    标签: python r plot popen


    【解决方案1】:

    添加:

    show()
    

    之后:

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

    【讨论】:

    • 对不起,当我这样做时,我在 stderr 中收到以下错误:'.getClassFromCache(Class, where) 中的错误:缺少参数“object”,没有默认调用:show ... show - > showDefault -> getClass -> .getClassFromCache Execution halted' 如果我使用 'show(plot(x,y, type="o"))' 也会发生同样的事情。没有输出。
    猜你喜欢
    • 2019-12-12
    • 2018-04-27
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 2022-07-22
    相关资源
    最近更新 更多