Problem Using reticulate

As far as I knew, previously we cannot use the variables defined in python code chink in r chunks afterwards.

I went through this vignette and they said it can be done now using reticulate. But I can't seem to apply it.

I used the following:

---
title: "Example"
output: html_document
---

```{r setup, include=FALSE}
library(reticulate)
```

```{python}
x = 5
```

```{python}
print(x)
```

```{r}
print(py$x)
```

Now, if I run all the chunks, the 2nd python chunk can't detect the x variable defined in 1st python chunk and shows an error. Nether does the R chunk, as it returns NULL. (Check the left part of the screenshot below)

However,if I knit the file, I get I get the correct output. (On right part of the screenshot)

My question is this the expected behaviour, or am i missing something?

FYI, I'm using Windows 10 64 bit with python 3.6.5 64 bit (not anaconda).

Thanks

You might need to upgrade to RStudio v1.2 in order to get reticulate going in R Notebooks (which are how chunks run inline in RStudio!). I'm guessing the current version of RStudio uses the old Python engine?

Well, my problem is not solved by upgrading to the RStudio 1.2.637.

I used the same document as before.

When I run all the chunks one by one, it works perfectly.

Also, knitting the document is fine, as before.

Now, if I use Run All or Restart R and Run All Chunks, it crashes Rstudio. Check the screenshots.

Any suggestions?

1 Like

Hmm, IDK :frowning: Maybe one of the RStudio staff can take a look at this?

Thanks for the bug report, @Yarnabrina. Unfortunately, I'm not able to reproduce; neither with the official Windows build of Python nor with an Anaconda installation -- your document runs fine on my Windows VM. Can you share some more details on your system? What is the output of:

sessionInfo()
py_config()

Thanks for the reply. Here's the info.

> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252   
[3] LC_MONETARY=English_India.1252 LC_NUMERIC=C                  
[5] LC_TIME=English_India.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.5.0 tools_3.5.0   
> reticulate::py_config()
python:         C:\Users\Anirban\AppData\Local\Programs\Python\Python36\python.exe
libpython:      C:/Users/Anirban/AppData/Local/Programs/Python/Python36/python36.dll
pythonhome:     C:\Users\Anirban\AppData\Local\Programs\Python\Python36
version:        3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\Anirban\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy
numpy_version:  1.14.3

python versions found: 
 C:\Users\Anirban\AppData\Local\Programs\Python\Python36\python.exe
 C:\Users\Anirban\AppData\Local\Programs\Python\Python36\\python.exe

Mine looks pretty much the same:

python:         C:\Users\kevin\AppData\Local\Programs\Python\Python36\\python.exe
libpython:      C:/Users/kevin/AppData/Local/Programs/Python/Python36/python36.dll
pythonhome:     C:\Users\kevin\AppData\Local\Programs\Python\Python36
version:        3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\kevin\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy
numpy_version:  1.14.3

python versions found: 
 C:\Users\kevin\AppData\Local\Programs\Python\Python36\\python.exe
 C:\Users\kevin\ANACON~1\python.exe
 C:\Users\kevin\Anaconda3\python.exe

so I'm curious what could be causing the underlying problem. Do you have a .Rprofile setting any reticulate-related settings or can you think of anything else that might be different on your platform?

In some rare cases, we've seen programs on the PATH interfere with R / RStudio's normal functioning (e.g. if an incompatible make is on the PATH). That seems unlikely to be the issue here, but it might be worth checking out.

Finally, can you also share a diagnostics report, just in case any extra information is there? https://support.rstudio.com/hc/en-us/articles/200321257-Running-a-Diagnostics-Report

Nope. I never made any change in .Rprofile myself.

I don't think so. The programs in my path are Rtools, MikTeX, Python and Pandoc.

And, this is the diagnostics report.

diagnostics-report.pdf (290.6 KB)

I am confused regarding this problem as Run All fails, while running the chunks separately works. Don't these two perform the same?

Within RStudio, we have some extra machinery around chunk execution that allows us to capture outputs and display them in the IDE, so the codepaths are not entirely the same.

Some of the entries in your diagnostics report suggest that some of the notebook-related files are locked while RStudio attempts to use them. Do you have an over-aggressive antivirus, or something like Dropbox attempting to synchronize remotely running at the same time? One error also suggests the machine may have run out of memory or disk space, in case that could be an issue.

Currently, I am not using any antivirus, other than Windows Defender (with just the default settings).

Also, my working directory is not being synchronized to the cloud.

I have more than 100 GB left in my hard drive, and I don't think that a simple program like this can consume my entire 8 GB RAM.

Just for your information, I installed Rstudio in a new machine, and the exact same error repeated, with both anaconda and native python.

The session info for this machine is,

> library(reticulate)
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] reticulate_1.7

loaded via a namespace (and not attached):
[1] compiler_3.5.0  Matrix_1.2-14   tools_3.5.0     Rcpp_0.12.17    grid_3.5.0      jsonlite_1.5    lattice_0.20-35
> py_config()
python:         C:\Users\aniray\AppData\Local\Programs\Python\Python36\python.exe
libpython:      C:/Users/aniray/AppData/Local/Programs/Python/Python36/python36.dll
pythonhome:     C:\Users\aniray\AppData\Local\Programs\Python\Python36
version:        3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\aniray\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy
numpy_version:  1.14.3

python versions found: 
 C:\Users\aniray\AppData\Local\Programs\Python\Python36\python.exe
 C:\Users\aniray\AppData\Local\Programs\Python\Python36\\python.exe
 C:\PROGRA~3\ANACON~1\python.exe
 C:\ProgramData\Anaconda3\python.exe
 C:\ProgramData\Anaconda3\envs\tensorflow\python.exe