Reticulate not sharing state between R/Python cells or Python/Python cells in RMarkdown

扶醉桌前 提交于 2019-12-08 17:06:09

问题


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:

Cell 1 (Setup):

{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)

Output:

[1] TRUE

Cell 2 (set variable in Python):

{python}
x = 2

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

{r}
py$x

Output:

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

Cell 4 (set variable in R):

{r}
x <- 2

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

{python}
r.x

Output:

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

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

{python}
x

Output:

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

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!


回答1:


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.




回答2:


This is fixed in current RStudio e.g. 1.2.1114. But if you are like me stuck with RStudio Server Pro 1.1.456 a workaround is to use reticulate::repl_python() to run python chunks by copy pasting them into a python console. You can close and open the console again if you need to run an R chunk in between - the state will be maintained. When you are done hacking around you can knit the whole file without problems.



来源:https://stackoverflow.com/questions/49324289/reticulate-not-sharing-state-between-r-python-cells-or-python-python-cells-in-rm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!