Use reticulate to access an R object in a new environment

308 views Asked by At

The following gives the expected result (executed in the global environment):

library(reticulate)

x <- 1
py_run_string("r.x = 2")

#> x
#[1] 2

However, when I try to access an object from a new environment, the python code is "run" in my global environment instead:

library(reticulate)
e <- new.env()
assign("y", 1, e)

eval(py_run_string("r.y = 2"), envir = e)

#> e$y
#[1] 1
#> y
#[1] 2

Is there any way to manipulate an object from a separate R environment with python code? Maybe reticulate isn't the correct tool for this?

1

There are 1 answers

0
ha-pu On

I wanted to do the same thing. Based on this response from R Studio, I believe that the r object in reticulate only accesses .GlobalEnv.