如何使用参数从R调用python脚本

时间:2022-09-18 20:29:41

I have a python script which takes some 5 arguments( a filename, 3 int values and 2 float values). I need to call this python script from R. How can I do that. I am trying to use rPython, but it doesnt allow me to pass the argument

我有一个python脚本,它需要5个参数(一个文件名,3个int值和2个浮点值)。我需要从R调用这个python脚本。我该怎么做。我正在尝试使用rPython,但它不允许我传递参数

library("rPython")
python.load("python scriptname")

I dont know how to pass the arguments

我不知道如何传递参数

from command line, i run my python script like:

从命令行,我运行我的python脚本,如:

python scriptname filename 10 20 0.1 5000 30

2 个解决方案

#1


15  

You can invoke a system command

您可以调用系统命令

system('python scriptname')

To run the script asynchronously you can set the wait flag to false.

要异步运行脚本,可以将wait标志设置为false。

system('python scriptname filename 10 20 0.1 5000 30', wait=FALSE)

The arguments that get passed as they would in command line. You will have to use sys.argv in the python code to access the variables

在命令行中传递的参数。您将不得不在python代码中使用sys.argv来访问变量

#test.py
import sys

arg1 = sys.argv[1]
arg2 = sys.argv[2]
print arg1, arg2

The R command below would output 'hello world'

下面的R命令会输出'hello world'

system('python test.py hello world', wait=FALSE)

#2


8  

There is a small typo in the great previous answer. The right code is the following:

在之前的伟大回答中有一个小错字。正确的代码如下:

 system('python test.py hello world', wait = FALSE)

where wait is FALSE (not wait=Flase or wait=False)

其中wait为FALSE(不等待= Flase或wait = False)

#1


15  

You can invoke a system command

您可以调用系统命令

system('python scriptname')

To run the script asynchronously you can set the wait flag to false.

要异步运行脚本,可以将wait标志设置为false。

system('python scriptname filename 10 20 0.1 5000 30', wait=FALSE)

The arguments that get passed as they would in command line. You will have to use sys.argv in the python code to access the variables

在命令行中传递的参数。您将不得不在python代码中使用sys.argv来访问变量

#test.py
import sys

arg1 = sys.argv[1]
arg2 = sys.argv[2]
print arg1, arg2

The R command below would output 'hello world'

下面的R命令会输出'hello world'

system('python test.py hello world', wait=FALSE)

#2


8  

There is a small typo in the great previous answer. The right code is the following:

在之前的伟大回答中有一个小错字。正确的代码如下:

 system('python test.py hello world', wait = FALSE)

where wait is FALSE (not wait=Flase or wait=False)

其中wait为FALSE(不等待= Flase或wait = False)