通过 subprocess.Popen 在 python 中执行 R 脚本 [英] Executing an R script in python via subprocess.Popen

查看:46
本文介绍了通过 subprocess.Popen 在 python 中执行 R 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 R 中执行脚本时,它是:

When I execute the script in R, it is:

$ R --vanilla --args test_matrix.csv < hierarchical_clustering.R > out.txt

在 Python 中,如果我使用:

In Python, it works if I use:

process = subprocess.call("R --vanilla --args "+output_filename+"_DM_Instances_R.csv < /home/kevin/AV-labels/Results/R/hierarchical_clustering.R > "+output_filename+"_out.txt", shell=True)

但是这个方法没有提供process.wait()函数.

But this method doesn't provide the process.wait() function.

所以,我想使用subprocess.Popen,我试过:

So, I would like to use the subprocess.Popen, I tried:

process = subprocess.Popen(['R', '--vanilla', '--args', "\'"+output_filename+"_DM_Instances_R.csv\'",  '<', '/home/kevin/AV-labels/Results/R/hierarchical_clustering.R'])

但是没有用,Python只是打开了R但是没有执行我的脚本.

But it didn't work, Python just opened R but didn't execute my script.

推荐答案

给它 Rscript 的路径而不是 'R'.我有同样的问题.打开 R 但不执行我的脚本.您需要调用 Rscript(而不是 R)来实际执行脚本.

Instead of 'R', give it the path to Rscript. I had the same problem. Opens up R but doesn't execute my script. You need to call Rscript (instead of R) to actually execute the script.

retcode = subprocess.call("/Pathto/Rscript --vanilla /Pathto/test.R", shell=True)

这对我有用.

干杯!

这篇关于通过 subprocess.Popen 在 python 中执行 R 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆