I have created a RMarkdown document with some objects in a Python code chunk. I'd like to be able to see these objects in the Environment window list in RStudio like the objects created in a R code chunk.
For instance, if I create a vetor, say:
a = c(1,2,3,4,5)
in a R code chunk (i.e. if I wrap the code with {r}
, it will appear in the Environment window list.
On the other hand, if I create another vector in a Python code chunk, like the one below:
b = [1,2,3,4,5]
b will not be listed in the Environment window. At least, I was not able to find a way to have it shown there.
On the other hand, b can be accessed in a R code chunk by prefixing it with "py$" like "py$b"
Is there a way to achieve this?
In any chunk that assigns to a Python variable b
, follow that assignment with a command like r.b = b
. Then b
and its value will appear in the Environment window.
r.
is the converse of py$
; it lets the Python chunk access the R namespace.
Reference: https://blog.rstudio.com/2018/03/26/reticulate-r-interface-to-python/
Edit: to be more concise, you could chain the assignment:
r.b = b = [1,2,3,4,5]
Not completely sure it's wise, though. The whole thing about Python chain assignments going left-to-right (see, e.g., https://stackoverflow.com/a/36346517) freaks me out. The point made there about two Python variables pointing to the same object should be kept in mind, although as far as I can tell, assigning a new value to b
in an R chunk doesn't change the value of py$b
.