小编Sea*_*ran的帖子

网格化不在RMarkdown中的R/Python单元格或Python/Python单元格之间共享状态

根据设置说明,我正在努力Reticulate工作RMarkdown.但是,我无法在单独的Python单元格或Python和R单元格之间共享状态,因为文档表明我应该能够.这是我的设置和输出:

单元格1(设置):

{r}
library(reticulate)
path_to_python <- "/Users/User/anaconda3/bin/python"
use_python(path_to_python)
knitr::knit_engines$set(python = reticulate::eng_python)
py_available(initialize = TRUE)
Run Code Online (Sandbox Code Playgroud)

输出:

[1] TRUE
Run Code Online (Sandbox Code Playgroud)

Cell 2(在Python中设置变量):

{python}
x = 2
Run Code Online (Sandbox Code Playgroud)

单元格3(尝试访问R中的Python变量):

{r}
py$x
Run Code Online (Sandbox Code Playgroud)

输出:

Error in py_get_attr_impl(x, name, silent) : AttributeError: module '__main__' has no attribute 'x'
Run Code Online (Sandbox Code Playgroud)

单元格4(在R中设置变量):

{r}
x <- 2
Run Code Online (Sandbox Code Playgroud)

单元格5(尝试在Python中访问R变量):

{python}
r.x
Run Code Online (Sandbox Code Playgroud)

输出:

Traceback (most recent call last):
  File "/var/folders/2b/dgy6vs4n3lbfy2xqwc3gqq9m0000gn/T/RtmpTqIR6P/chunk-code-108b44104ec28.txt", line 1, in <module> r.x NameError: name 'r' is not defined
Run Code Online (Sandbox Code Playgroud)

单元格6(尝试在后续Python单元格中访问以前的Python变量):

{python}
x
Run Code Online (Sandbox Code Playgroud)

输出:

Traceback (most recent call last): …
Run Code Online (Sandbox Code Playgroud)

python r r-markdown reticulate

9
推荐指数
1
解决办法
1009
查看次数

标签 统计

python ×1

r ×1

r-markdown ×1

reticulate ×1