Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve: "Error: object 'py' not found" in Rmarkdown when calling variable form python chunk to R chunk

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

like image 768
Shreya Chatterjee Avatar asked Feb 07 '26 01:02

Shreya Chatterjee


2 Answers

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.

like image 90
Matt Dancho Avatar answered Feb 12 '26 06:02

Matt Dancho


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

library(reticulate)
py$a
like image 40
arnle Avatar answered Feb 12 '26 06:02

arnle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!