简体   繁体   English

R Reticulate - 以编程方式将定义的变量从 Python 环境移动到 R

[英]R Reticulate - Moving defined variables programmatically from Python environment to R

Caue:原因:

I'm creating dataframes programmatically in Python using globals() .我正在使用globals()在 Python 中以编程方式创建数据框。

In the below code, I'm creating 5 datasets that starts with a 'PREFIX' in caps, followed by a letter then ending with a suffix.在下面的代码中,我创建了 5 个数据集,它们以大写的“PREFIX”开头,后跟一个字母,然后以一个后缀结尾。

R R

library(reticulate)
repl_python()

Python Python

import os
import pandas as pd

letters = ('a','b','c','d','e')
df_names = []

for ele in letters:
  globals()['PREFIX_{}_suffix'.format(ele)] = pd.DataFrame(columns = ['col_a', 'col_b']).astype(str)
  df_names.append(['PREFIX_{}_suffix'.format(ele)][0])
print(df_names)
['PREFIX_a_suffix', 'PREFIX_b_suffix', 'PREFIX_c_suffix', 'PREFIX_d_suffix', 'PREFIX_e_suffix']

Request:要求:

I would like to select dataframes starting with a prefix (ideally with regular expression ^PREFIX ) and move those specific dataframes from reticulate's python environment to R environment programmatically.我想以前缀开头的 select 数据帧(最好使用正则表达式^PREFIX )并将这些特定数据帧从网状的 python 环境编程移动到 ZE1E1D3D40573127E9EE0480CAF18 环境

For the sake of the task, I have added the dataframes variable names into df_names .为了完成任务,我已将数据帧变量名称添加到df_names中。 However, using regex is highly encouraged.但是,强烈建议使用正则表达式。

I know the variables are stored in py object that can be accessed with a $ .. but I'm not sure how to select dataframes iteratively and move those dataframes from python's environment to R's environment programmatically all at once.我知道变量存储在py object 中,可以使用$ .. 访问,但我不确定如何迭代地访问 select 数据帧,并以编程方式一次将这些数据帧从 python 环境移动到 R 环境。


In R, I usually use ls(pattern=<regex>) to select objects in R environment.在 R 中,我通常在 R 环境中使用ls(pattern=<regex>)到 select 对象。

In Python, you can list the variables using locals() , see this thread .在 Python 中,您可以使用locals()列出变量,请参阅此线程

This thread discuss passing python functions from R to python.线程讨论将 python 函数从 R 传递到 python。

Here is my solution using regex:这是我使用正则表达式的解决方案:

In python:在 python 中:

  • Create your regex pattern创建您的正则表达式模式
  • Apply your pattern to dir() output, which prints the defined variables in your python's environment将您的模式应用于dir() output,它会在您的 python 环境中打印定义的变量
  • Save selected fetched dataframes in a list将选定的提取数据框保存在列表中
import os
import re

r = re.compile("^PREFIX")
py_dfs = list(filter(r.match, dir())) # fetch defined variables from python's env
print(py_dfs)
['PREFIX_a_suffix', 'PREFIX_b_suffix', 'PREFIX_c_suffix', 'PREFIX_d_suffix', 'PREFIX_e_suffix']

In R:在 R 中:

  • Access that list in python that has the dataframe names访问 python 中具有 dataframe 名称的列表
  • Using R's reticulate::py_eval evaluate your python object converting it to r using reticulate::py_to_r使用 R 的reticulate::py_eval评估您的 python object 使用reticulate::py_to_r
  • Using assign to assign dynamic defined variables with the same name of the dataframe in python使用assign对python中的dataframe name的动态定义变量赋值
for (df in py$py_dfs){
  name  = df
  r_df = py_to_r(py_eval(df))
  assign(paste0(name), r_df)
}

> ls(pattern="^PREFIX")
[1] "PREFIX_a_suffix" "PREFIX_b_suffix" "PREFIX_c_suffix" "PREFIX_d_suffix" "PREFIX_e_suffix"
> dim(PREFIX_a_suffix)
[1] 0 2
> class(PREFIX_a_suffix)
[1] "data.frame"
> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM