6

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.

Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110
  • It's related to the third comment by pjcrosbie in this knitr bug request: [Python environment persistance across chunks #1440](https://github.com/yihui/knitr/issues/1440). – Paul Rougieux Jun 27 '18 at 14:11
  • 1
    Gists can disappear or change over time, could you include the (current) source of the Rmarkdown document to keep this question self-sufficient? – r2evans Jun 27 '18 at 14:34
  • 1
    If a question is related to RStudio, please state its version. Without that information, a blind guess is that the latest (preview) version might have fixed your problem: https://www.rstudio.com/products/rstudio/download/preview/ – Yihui Xie Jun 28 '18 at 15:00

1 Answers1

3

This functionality works in Rstudio preview 1.2.747. Question kept for historical purposes.

Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110