reticulate允许您从 R 与 Python 交互。在 Python 中,通常使用(类)方法与变量进行交互。使用 reticulate 时如何访问/执行 R 中一个 Python 变量的方法?例如,如果我创建以下 Python 字典:
```{python}
fruits = {
"apple": 53,
"banana": None,
"melon": 7,
}
```
可以使用 reticulate 访问,
```{r}
py$fruits
```
## $apple
## [1] 53
##
## $banana
## NULL
##
## $melon
## [1] 7
如何从字典类(例如keys()
R)中调用其中一种方法?
```{python}
print(fruits.keys())
```
## dict_keys(['apple', 'banana', 'melon'])
我试过了:
```{r error=TRUE}
py$fruits$keys()
```
## Error in eval(expr, envir, enclos): attempt to apply non-function
```{r error=TRUE}
py$fruits.keys()
```
## Error in py_get_attr_impl(x, name, silent): AttributeError: module '__main__' has no attribute 'fruits.keys'
但两次尝试都失败了。