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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With