```{r}
library(reticulate)
```
Python NameErrors
Issue
I keep getting NameErrors and weird behavior with mixed R and python chunks. Usually it seems to be that python can’t find objects if there’s an R chunk in between. It only happens when I render in quarto- running this interactively in Rstudio always works fine.
I’ve set the RETICULATE_PYTHON
and QUARTO_PYTHON
environment variables, and put engine: knitr
in the yaml, and it doesn’t seem to have helped.
The issue seems to be worst when R is somehow involved with the objects.
This doc is to try to systematically and simply reproduce the issue. I hope that works.
It was working, but the more often I rendered, the more intermittent the problem became. I’ve noted something about the frequency of errors for the chunks- some error more than others.
An R chunk to make sure knitr kicks off
```{r}
<- 1
a ```
Define a bunch of python variables to do different things with.
```{python}
= 2
b = 3
c = 4
d = 5
e = 6
f = 7
g ```
Can we see those immediately?
```{python}
b```
2
What if there’s an R chunk in the middle?
```{r}
<- 1
rdummy ```
Can we still see the python? Yes
```{python}
c```
3
What if the R touches the python?
```{r}
<- py$d + 1
rb
rb```
[1] 5
This now fails almost every time
```{python}
d```
4
Are the other variables unscathed? No, this also fails consistently (but not 100% of the time).
```{python}
c```
3
What about those that haven’t been used since declared? Fails again most of the time.
```{python}
e```
5
Can we declare more python?
```{python}
= 6
f ```
again, R in the middle
```{r}
<- 5
rtest ```
Does the new python persist?
```{python}
f```
6
What if there’s a python chunk that accesses R?
```{python}
= r.a + 1
g
g```
2.0
Can we access the previous python variables? Sometimes. I intermittently get an error here, but sometimes it runs.
```{python}
f```
6
Can we access that new python variable? Also only sometimes.
```{python}
g```
2.0
What if we declare python in two chunks? Do they all get annihilated after crossing the language boundary, or only on the basis of their chunk declaration?
```{python}
= 7
g = 8
h ```
```{python}
= 9
j ```
Contaminate with R
```{r}
<- py$g + 9
rgh
rgh```
[1] 16
Can we see the python that was defined with g
? This works sometimes. So whatever is going on is unstable. It’s strange that this and the following two work sometimes- they seem like the same thing that consistently fails above (though that’s now working too).
```{python}
h```
8
I wasn’t expecting that to work (and it only does sometimes). Can we see g
itself? Sometimes
```{python}
g```
7
Can we get to the python that was defined in a different chunk? Sometimes.
```{python}
j```
9
Is the issue that we didn’t ask for the touched variable first? Do the same thing, but this time ask for the contaminated variable
```{python}
= 10
k = 11
l ```
```{python}
= 12
m ```
Access in R
```{r}
<- py$k + 9
r_m ```
Is that variable there in python?
```{python}
k```
10
Is the other one that’s defined with it?
```{python}
l```
11
How about the one in the other code chunk?
```{python}
m```
12
I’m not really sure what to do with this. Anything defined before any interaction across languages (either py$pyvar
or r.rvar
) would die in python when I rendered this the first few times, but now it’s all working after about 10 update renders. I’m throwing execute: error: true
in all the R-py yaml headers, but that doesn’t actually help when I actually want them to run and there are errors intermittently.