Variable are not retained from one chunk to the next in notebook mode, but they are retained when knitting the markdown document to html.
I made a sample document available as a gist called pythonvariables.Rmd, the content of this file is:
---
title: "R Notebook"
output:
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
```
```{python}
x = 1
```
```{python}
print(x)
```
```{r}
print(py$x)
```
In Rstudio version 1.1.453, in notebook mode, when running one chunk after the other, the output of the print(x)
python chunk is:
Traceback (most recent call last):
File "C:\Users\rougipa\AppData\Local\Temp\2\RtmpQFW3Rj\chunk-code-1d44920f50.txt", line 1, in <module>
print(x)
NameError: name 'x' is not defined
However the issue doesn't appear when the Rmd is compiled to html. The output of the print(x)
python chunk is 1 as expected.
This functionality works in Rstudio preview 1.2.747. Question kept for historical purposes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With