简体   繁体   English

如何在 R Markdown 中将 R 多元素向量转换为 Python 列表?

[英]How can I convert an R multi-element vector to Python list in R Markdown?

I have to read some vector created in an R chunk in a Python chunk in an R Markdown document.我必须在 R Markdown 文档的 Python 块中读取在 R 块中创建的一些向量。

I successfully created the vector, but when I try to attribute the R vector to a Python variable via py_list = r.vector it just doesn't work.我成功创建了向量,但是当我尝试通过py_list = r.vector将 R 向量归因于 Python 变量时,它不起作用。

Here there is a peace of the code:这里有一个和平的代码:

{python ingestao_parcela, eval = py_eval, include = FALSE}
###### IMPORT FEATURE ######
name_layer_tmp = 'parcela_tmp'
arcpy.FeatureClassToFeatureClass_conversion('C:/Users/CLEITE/AppData/Roaming/ESRI/Desktop10.7/ArcCatalog/Connection to BRP01.sde/SDE_GEO.VANT_PARCELA', arcpy.env.workspace, name_layer_tmp, """ DT_MES_REF = {} """.format(dt_mes_ref))

##### CREATE UNIQUE CODE COLUMN #####
arcpy.AddField_management(name_layer_tmp, 'CONCAT_T', 'TEXT')
arcpy.CalculateField_management(name_layer_tmp, 'CONCAT_T',
""" [ESTADO_FEDERATIVO] & "_" & [CD_HORTO] & "_" & [CD_TALHAO] """, 'VB')

##### R VECTOR TO PYTHON LIST #####
dado_amostral_py = r.dado_amostral
dado_amostral_py = tuple(dado_amostral_py)

##### FEATURE FILTER #####
arcpy.FeatureClassToFeatureClass_conversion('parcela_tmp', arcpy.env.workspace,
'parcela', """ [CONCAT_T] IN {} """.format(dado_amostral_py))

PS: I already checked if the R vector "dado_amostral" was created in R indeed, and it was. PS:我已经检查过 R 向量“dado_amostral”是否确实是在 R 中创建的,确实如此。

I already checked the reticulate CRAN page and I found something about element types conversion.我已经检查了网状 CRAN 页面,我发现了一些关于元素类型转换的信息。

What I understand from this article is that if I want to use some object created in R inside a Python chunk I only would have to do like I wrote in the beginning: py_list = r.vector .我从这篇文章中了解到,如果我想在 Python 块中使用在 R 中创建的某个对象,我只需要像我在开头写的那样: py_list = r.vector

But when I try to run the R Markdown document, it says that there is no variable such "dado amostral".但是当我尝试运行 R Markdown 文档时,它说没有像“dado amostral”这样的变量。

Am I missing something?我错过了什么吗?

I've just figured out how to do what I wanted.我刚刚想出了如何做我想做的事。

The solution I found is:我找到的解决方案是:

{r}
r_to_py(dado_amostral)
{python}
py_dado_amostral = r.dado_amostral

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

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