0

I want to pass a variable that's not in the R global environment into a python chunk (using reticulate). Here is an example:

library(reticulate)

newEnv <- new.env()
newEnv$x <- 9

z <- 3
print(r.newEnv$x)

This yields an error. Note that I also tried print(r.newEnv[['x']]). Also, print(r.z) gives 3.0, as expected. Finally, I've played around with stuff like eval() and constructing strings to pass to Python and so far no dice.

Phil
  • 7,287
  • 3
  • 36
  • 66
user1713174
  • 307
  • 2
  • 7

1 Answers1

0

It kind of works if you copy your second Env into a list first:

```{r}
library(reticulate)

newEnv <- new.env()
newEnv$x <- 9
newEnv$df <- data.frame(1:10,1:10)

newEnvCopyDummy <- as.list(newEnv)

z <- 3
```

And then your

```{python}
print(r.newEnvCopyDummy)
```

returns:

{'x': 9.0, 'df':    X1.10  X1.10.1
0      1        1
1      2        2
2      3        3
3      4        4
4      5        5
5      6        6
6      7        7
7      8        8
8      9        9
9     10       10}

But admittedly, this is not exactly what you wanted.

Max Teflon
  • 1,760
  • 10
  • 16
  • Thanks for the comprehensive answer! It all works fine if I copy the environment-specific objects into the R global environment, so I can always continue doing that (but that, of course, sort of defeats the purpose of having that other environment in the first place). – user1713174 Jun 11 '20 at 17:19