17

Maybe I'm missing something, but if the following code is the content of my Rmd file

```{r}
library(reticulate)
use_virtualenv("r-reticulate")
py_available(TRUE)
```
```{python}
a = 7
print(a)
```
```{r}
py$a
```

when I Knit the file, the output for the last chunk is 7 (as expected). On the other hand, clicking the run all button in Rstudio (or running chunks one by one), results on NULL for the last chunk.

Comparing with the R notebook example it seems like assigning something to flights in the python chunk should make py$flights available for R, but that doesn't seem the case.

Questions:

  1. Is there a way to access from R a variable created in a Python chunk previously ran (not knit)? How to "export" to R a variable created within a python chunk?
  2. What is a good reference to understand what happens when I click the run button in a python chunk of a Rmarkdown file?

EDIT: Ok so after seeing the first answers here, I did update both knitr and rmarkdown to the latest version, but still had the same problem. I added py_available(TRUE) to my file to make sure it was initialized, still, last chunk results in 7 when knitted, but running chunks one-by-one results in

> py$a
Error in py_get_attr_impl(x, name, silent) : 
  AttributeError: 'module' object has no attribute 'a'

The problem is: Assigning a value to a in the python chunk isn't doing anything to py$a in the R environment. Maybe this "shared" environment between R and python isn't how the package is supposed to work? Also, for some extra information

> py_config()
python:         /usr/bin/python
libpython:      /usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
pythonhome:     /usr:/usr
version:        2.7.14 (default, Sep 23 2017, 22:06:14)  [GCC 7.2.0]
numpy:          /usr/lib/python2.7/dist-packages/numpy
numpy_version:  1.12.1

python versions found: 
 /usr/bin/python
 /usr/bin/python3
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
VFreguglia
  • 2,129
  • 4
  • 14
  • 35
  • [This blog post from (today/yesterday)](https://blog.rstudio.com/2018/03/26/reticulate-r-interface-to-python/) by the `reticulate` authors may be useful – SymbolixAU Mar 27 '18 at 01:56
  • 2
    The docs you link to say that reticulate is in the RStudio daily builds, not sure if it's actually made it to released versions of RStudio. RStudio already included a way to run Python chunks by just using the default `python` interpreter, but no cross-communication with R, that might be what you're seeing. – Marius Mar 27 '18 at 02:36
  • 1
    does it wor for you by know? – Tonio Liebrand Mar 30 '18 at 15:08
  • Still doesn't work, just edited the post with the information. – VFreguglia Mar 30 '18 at 15:48

3 Answers3

10

Rmarkdown / knitr:

Running the chunks:

Running the chunks without knitting the document is not supported so far. See here: https://github.com/yihui/knitr/issues/1440 or Reticulate not sharing state between R/Python cells or Python/Python cells in RMarkdown.

Edit: Workaround by Freguglia:

"Workaround is to turn python chunks into R chunks and just wrap the whole content in the py_run_string() function, so whatever you assign in that piece of code is accessible from R by py$variable_name."

Knitting the document:

One way is to upgrade knitr as suggested above, but you dont have to and you also dont need RStudio daily build.

If you have a version of knitr prior to 1.18, you can include:

```{r setup, include = FALSE} knitr::knit_engines$set(python = reticulate::eng_python) ``` , see here: https://rstudio.github.io/reticulate/articles/r_markdown.html#engine-setup.

Python:

If it doesnt work ensure the python connection is running outside of rmarmdown/knitr: py_run_string("x = 10"); py$x.

In case that also doesnt work, you should check: py_available() and py_numpy_available().

If it returns FALSE: Try to initialize it with: py_available(TRUE).

If that´s still a no - check your config: py_config()

It will give you further hints on the problem:

Examples for me were: different bit versions of R and python (32 vs 64) or somehow i ran into trouble having installed both Python2.7 and seperately Anaconda.

Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
  • `py_run_string("x = 10"); py$x` works fine. It seems like `py_run_string()` is able to modify `py` in the R environment, while just running a python is unable to modify anything. – VFreguglia Mar 30 '18 at 15:50
  • to be clear: add the `knitr::knit_engines$set(...)` snippet and knitting the document still doesnt work? (Running the chunks without knitting is not supported so far,...) – Tonio Liebrand Mar 30 '18 at 15:59
  • 1
    Knitting the document works perfectly. I thought there was a way of executing chunks individually and "export" the variables to the R environment. This is not possible so far as you stated, also found the same here https://github.com/yihui/knitr/issues/1440. – VFreguglia Mar 30 '18 at 16:06
  • 1
    I accepted this answer, but I also want to add that a workaround is to turn python chunks into R chunks and just wrap the whole content in the `py_run_string()` function, so whatever you assign in that piece of code is accessible from R by `py$variable_name`. – VFreguglia Mar 30 '18 at 16:09
  • 1
    good point, i added it and quoted you... You could also add your workaround here: https://stackoverflow.com/questions/49324289/reticulate-not-sharing-state-between-r-python-cells-or-python-python-cells-in-rm/49435461#49435461 – Tonio Liebrand Mar 30 '18 at 17:05
6

This is fixed in current RStudio Desktop e.g. 1.2.1114. But if you are like me stuck with RStudio Server Pro 1.1.456 a better workaround than using py_run_string might be to use reticulate::repl_python() which gives you a Python console within the R console and allows you to run your python chunks by copy pasting them into the console.

workaround: workaround working: working

jan-glx
  • 7,611
  • 2
  • 43
  • 63
  • I tried running the same code from the question in the daily build. While something has certainly changed, running the python chunk throws an error now. – VFreguglia Feb 05 '19 at 19:41
  • I just clarified that it is fixed in Desktop (windows). Which RStudio are you using exactly? – jan-glx Feb 05 '19 at 20:01
5

You must use the Rstudio daily build (source) and upgrade knitr, rmarkdown to the latest version.

> packageVersion("rmarkdown")
[1] ‘1.9’
> packageVersion("knitr")
[1] ‘1.20’
Heisenberg
  • 8,386
  • 12
  • 53
  • 102