3

I am using R markdown and reticulate package.After running a simple 2 line python code in a chunk,I cant use the python variable in the R chunk using the py$x function.I get the following error: "Error: object 'py' not found"

I have already tried another function py_to_r for converting python objects to R but even that doesnt work cause it throws an error that it cant find the object.I am including python configuration also:

    ```{r}
    py_config()
    ```


python:         C:\Users\chatsh02\AppData\Local\CONTIN~1\ANACON~1\python.exe
libpython:      C:/Users/chatsh02/AppData/Local/CONTIN~1/ANACON~1/python36.dll
pythonhome:     C:\Users\chatsh02\AppData\Local\CONTIN~1\ANACON~1
version:        3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\chatsh02\AppData\Local\CONTIN~1\ANACON~1\lib\site-packages\numpy
numpy_version:  1.14.2

python versions found: 
 C:\Users\chatsh02\AppData\Local\CONTIN~1\ANACON~1\python.exe
 C:\Users\chatsh02\AppData\Local\Continuum\anaconda3\python.exe
 C:\Users\chatsh02\AppData\Local\Continuum\anaconda3\envs\r-reticulate\python.exe


```{r}
library(reticulate)
use_virtualenv('r-reticulate')
py_available(TRUE)
```

```{python}
a=7
print(a)
```

```{r}
py$a
```

Expected results should be I should be getting R variable a with value 7.Actual result: Error: object 'py' not found

3 Answers3

5

I got this because I forgot to run library(reticulate) inside my Rmarkdown Document. It looks like you have it, but just make sure you actually run that line.

Matt Dancho
  • 6,840
  • 3
  • 35
  • 26
1

be sure to call the reticulate package in the same chunk, that works for me.

library(reticulate)
py$a
arnle
  • 480
  • 4
  • 10
0

You just have to knit your document, then everything works fine. Executing the single chunks will not work, just by knitting them to HTML, PDF or whatever.

enter image description here

J_F
  • 9,956
  • 2
  • 31
  • 55