Python单元格之间网状不共享状态

Python单元格之间网状不共享状态

本文介绍了在RMarkdown中的R/Python单元格或Python/Python单元格之间网状不共享状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照设置说明尝试使ReticulateRMarkdown中工作.但是,我无法在单独的Python单元格或Python和R单元格之间共享状态,正如文档指示的那样.这是我的设置和输出:

I'm trying to get Reticulate working in RMarkdown, per the setup instructions. However, I am unable to share state between separate Python cells or Python and R cells, as the docs indicate I should be able to. Here is my setup and output:

单元格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)

输出:

[1] TRUE

单元2(在Python中设置变量):

Cell 2 (set variable in Python):

{python}
x = 2

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

Cell 3 (attempt to access Python variable in R):

{r}
py$x

输出:

Error in py_get_attr_impl(x, name, silent) : AttributeError: module '__main__' has no attribute 'x'

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

Cell 4 (set variable in R):

{r}
x <- 2

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

Cell 5 (attempt to access R variable in Python):

{python}
r.x

输出:

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

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

Cell 6 (attempt to access previous Python variable in subsequent Python cell):

{python}
x

输出:

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

任何帮助或建议将不胜感激!我已经尝试将reticulate指向不同的Conda环境和Python安装,但是没有运气.谢谢!

Any help or advice would be much-appreciated! I have already attempted pointing reticulate at different Conda environments and Python installations with no luck. Thanks!

推荐答案

我想我已经知道了.我误解了网状文档,认为它可以在RStudio中以交互方式在Python单元之间共享状态.在Github上浏览了未解决的问题后,似乎仍在进行RStudio集成.直接使用knitr编织文档时,可以得到单元格之间共享状态的预期行为.

I think I've figured this out. I misunderstood the reticulate documentation, taking it to mean I could share state between Python cells interactively in RStudio. After perusing the open issues on Github, it appears RStudio integration is still being worked on. When employing knitr directly to knit a document, I get the expected behavior with shared state between cells.

这篇关于在RMarkdown中的R/Python单元格或Python/Python单元格之间网状不共享状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:48