简体   繁体   English

访问通过系统命令执行的R中python脚本的输出

[英]Access the output of python script in R executed through system command

My current question is follow up question to the link below. 我当前的问题是下面链接的后续问题。

Not able to import pandas in R 无法在R中导入熊猫

I have executed the python code in R with system command. 我已经用系统命令在R中执行了python代码。 Now at the end of python script I want to access the Dataframe created in R. One way is to save the Dataframe created in python with df.to_csv and then import it in R. But I am wondering any efficient way to directly access the output in R. 现在,在python脚本的结尾,我想访问R中创建的数据框。一种方法是使用df.to_csv保存在python中创建的数据框,然后将其导入R。但是我想知道是否有一种有效的方法可以直接访问输出在R中

x=system("/Users/ravinderbhatia/anaconda/bin/python /Users/ravinderbhatia/Downloads/Untitled3.py EMEA regulatory '10% productivity saves SOW'")

output dataframe is: 输出数据帧为:

description                       status region
10  10% productivity saves SOW   pending   EMEA
16  10% productivity saves SOW  approved   EMEA

X just contains 0/1(status). X仅包含0/1(状态)。 As mentioned above, how to access Dataframe directly in R without saving it. 如上所述,如何直接在R中访问Dataframe而不保存它。

Python script used is:


import pandas as pd 
import numpy as np
import sys


from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

arg1 = sys.argv[1]
arg2 = sys.argv[2]
arg3 = sys.argv[3]
print (arg1)
print (arg2)
print (arg3)

def get_similar_CRs(arg1, arg2,arg3):
##create dummy data
    cr_id=range(1,41)

    description=['change in design','More robust system required',
                 'Grant system adminstrator rights',
                 'grant access to all products',
                 'Increase the credit limit',
                 'EDAP Scenario',
                 'Volume prpductivity for NA 2015',
                 '5% productivity saves SOW',
                 'effort reduction',
                 'reduction of false claims',
                 'Volume productivity EMEA',
                 'Volume productivity for NA 2016',
                 '10% productivity saves SOW',
                ]
    region=['EMEA','Asia Pacific','UK']

    business=['card','secured loan','mortgage']
    type=['regulatory','system','audit']

    status=['pending','approved']

    data=pd.DataFrame()
    data['description']=np.random.choice(description, 40)
    data['cr_id']=cr_id
    data['region']=np.random.choice(region,40)
    data['business']=np.random.choice(business, 40)
    data['status']=np.random.choice(status,40)
    data['type']=np.random.choice(type,40)

    subset_data=data.loc[data.region == arg1]
    print (subset_data.head())
    subset_data=subset_data.loc[subset_data.type ==arg2]

    ##This has to be captured dynamically
    new_cr=arg3

    cr_list=data['description'].unique().tolist()

    similar_CR=[] ###global variable
#     for new_cr in new_cr_lis
    for cr in cr_list:
        result=similar(new_cr,cr)
        if result >=0.8:
            similar_CR.append(cr)

    temp=subset_data.loc[subset_data.description.isin(similar_CR)]
    temp=temp[['description','status','region']]
    return temp

temp= get_similar_CRs (arg1, arg2, arg3)

print temp

I suggest looking into the reticulate package (see the online vignette ). 我建议调查reticulate包装(请参阅在线插图 )。

You can run your file with py_run_file() and access the python main module with py . 您可以使用py_run_file()运行文件,并使用py访问python主模块。 So lets say your file is called "Untitled3.py" and the data frame it creates is called df , then 因此,假设您的文件名为“ Untitled3.py”,其创建的数据帧称为df ,然后

library(reticulate)

use_python("/Users/ravinderbhatia/anaconda/bin/python")

py_run_file("Untitled3.py")

py$df

Edit 编辑

Alternatively, you can import only the function from the python file and just call them from inside REg, have the python file as 或者,您可以仅从python文件中导入函数,然后仅在REg内部调用它们,将python文件作为

import pandas as pd 
import numpy as np
import sys
from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

def get_similar_CRs(arg1, arg2,arg3):
    ##create dummy data
    cr_id=range(1,41)

    description=['change in design','More robust system required',
                 'Grant system adminstrator rights',
                 'grant access to all products',
                 'Increase the credit limit',
                 'EDAP Scenario',
                 'Volume prpductivity for NA 2015',
                 '5% productivity saves SOW',
                 'effort reduction',
                 'reduction of false claims',
                 'Volume productivity EMEA',
                 'Volume productivity for NA 2016',
                 '10% productivity saves SOW',
                ]
    region=['EMEA','Asia Pacific','UK']

    business=['card','secured loan','mortgage']
    type=['regulatory','system','audit']

    status=['pending','approved']

    data=pd.DataFrame()
    data['description']=np.random.choice(description, 40)
    data['cr_id']=cr_id
    data['region']=np.random.choice(region,40)
    data['business']=np.random.choice(business, 40)
    data['status']=np.random.choice(status,40)
    data['type']=np.random.choice(type,40)

    subset_data=data.loc[data.region == arg1]
    print (subset_data.head())
    subset_data=subset_data.loc[subset_data.type ==arg2]

    ##This has to be captured dynamically
    new_cr=arg3

    cr_list=data['description'].unique().tolist()

    similar_CR=[] ###global variable
#     for new_cr in new_cr_lis
    for cr in cr_list:
        result=similar(new_cr,cr)
        if result >=0.8:
            similar_CR.append(cr)

    temp=subset_data.loc[subset_data.description.isin(similar_CR)]
    temp=temp[['description','status','region']]
    return temp

and then run 然后运行

library(reticulate)

# To install pandas and numpy in the regular python environment
py_install("pandas", "numpy")

py_run_file("Untitled3.py")

py$get_similar_CRs("EMEA", "regulatory", "10% productivity saves SOW")

#>                   description  status region
#> 2  10% productivity saves SOW pending   EMEA
#> 25 10% productivity saves SOW pending   EMEA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用system()命令从另一个python脚本执行时,如何访问python脚本中变量的值? - How to access the value of a variable in a python script when executed from another python script using system() command? 获取通过os.system()命令执行的命令的输出 - Fetching the output of a command executed through os.system() command 使用System()从R脚本调用Python命令 - Invoke Python command from R script with System() 如何使用命令执行python脚本 - How to executed python script with command 通过python运行系统命令不会产生相同的输出 - Running a system command through python not producing the same output 将最后执行的命令的输出存储在Expect脚本中 - store output of last executed command in expect script 使用os.system命令执行时,如何将python控制台输出重定向到QTextBox? - How to redirect the python console output when executed with a os.system command to a QTextBox? 处理 Python 中执行的系统命令错误 - Handling executed system command errors in Python Java类的输出通过Python脚本运行,而Python脚本又由Apache Web服务器执行 - Output of a Java class run through a Python script, that is in turn executed by an Apache web server 通过python执行时无法识别批处理命令 - Batch command not recognized when executed through python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM