11

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!

Sean Cochran
  • 171
  • 6
  • I'm not sure why you're seeing the error message that you see after your first call to python. I have a Mac with both python2 and python3. I get an error similar to yours when I use my python2, but when I use my python3, the document knits without problems. I don't have python expertise - are there startup files that are run when one calls python? – Fred Boehm Mar 21 '18 at 03:46

2 Answers2

6

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.

Sean Cochran
  • 171
  • 6
3

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.

jan-glx
  • 7,611
  • 2
  • 43
  • 63