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!