Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python environment persistance across chunks #1440

Closed
talegari opened this issue Oct 4, 2017 · 14 comments
Closed

Python environment persistance across chunks #1440

talegari opened this issue Oct 4, 2017 · 14 comments
Milestone

Comments

@talegari
Copy link

talegari commented Oct 4, 2017

Each python chunk starts a own session? Would it be a better idea to maintain a single python session while knitting?

Here is the output of the minimal reproducible result: repr.pdf

### Persistance across chunks with R and Python

Lets define x in R

```{r}
# running R
x <- 1
```

Does R remember?

```{r}
# running R
x
```

yes it does!

```{python}
# running python
y = 1
```

Does python remember?

```{python}
# running python
try:
  print(y)
except:
  print("I do not remember 'y'")
```

No, it does not.

----

```{r}
sessionInfo()
```

```{python}
import IPython
print(IPython.sys_info())
```
@yihui
Copy link
Owner

yihui commented Oct 4, 2017

This is a known issue and we will definitely improve it (hopefully by the end of this year).

@pjcrosbie
Copy link

fwiw, imho, this would mean many using python could switch from juypter to knitr, starting with me. many thanks

@yihui
Copy link
Owner

yihui commented Oct 15, 2017

We have started working on it: rstudio/reticulate#107

@yihui
Copy link
Owner

yihui commented Oct 23, 2017

Please test the dev version of reticulate. After devtools::install_github('rstudio/reticulate'), you can set up the python engine via this in your first R code chunk:

knitr::knit_engines$set(python = reticulate::eng_python)

Then later Python code chunks should be executed in the same Python session.

@pjcrosbie
Copy link

this installed but did not persist the Python session across chunks in an Rmd file on Windows x64

{r} code chunk

devtools::install_github('rstudio/reticulate')
knitr::knit_engines$set(python = reticulate::eng_python)

Skipping install of 'reticulate' from a github remote, the SHA1 (3f3c2277) has not changed since last install. Useforce = TRUEto force installation
trying:

{python} chunk

x = 99
print(x)

99

{python} chunk

print(x)

Traceback (most recent call last): File "C:\Users\pjc\AppData\Local\Temp\Rtmp46YyUn\chunk-code-f4c63c523eb.txt", line 1, in <module> print(x) NameError: name 'x' is not defined

@yihui
Copy link
Owner

yihui commented Oct 24, 2017

I cannot reproduce your issue on macOS:

```{r}
knitr::knit_engines$set(python = reticulate::eng_python)
```

```{python}
x = 99
print(x)
```

```{python}
print(x)
```

How exactly did you compile your Rmd document?

@pjcrosbie
Copy link

pjcrosbie commented Oct 24, 2017

my bad.

  1. compiles OK when knited, see eg-knitr-python-01.pdf
  2. my reported error is when evaluate chunks one at a time in Rmd doc in RStudio, or run preview of .Rmd in RStudio

Q. is (2) a targeted feature, i.e. persisting across chunks in .Rmd when evaluated individually or previewed in RStudio?

eg-knitr-python-01.pdf

@yihui
Copy link
Owner

yihui commented Oct 24, 2017

For now, we only support the Knit button to compile the whole report. If you want to run the code chunks one by one in the RStudio IDE, I guess we have not added the support yet. You may file a feature request https://github.com/rstudio/rstudio/issues. Thanks!

@pjcrosbie
Copy link

Thank you! Another great job. Appreciate having it in knitr; have filed request for RStudio.

@yihui
Copy link
Owner

yihui commented Oct 24, 2017

I didn't contribute anything this time. @kevinushey was our hero 😁

@kevinushey
Copy link

Support for this feature in RStudio with R Markdown documents / R Notebooks is forthcoming, but we wanted to get this ready for users ASAP regardless!

@yihui yihui added this to the v0.18 milestone Nov 17, 2017
@yihui yihui closed this as completed in b2d8929 Nov 17, 2017
@yihui
Copy link
Owner

yihui commented Nov 17, 2017

The python engine in the reticulate package has become the default in the current development version of knitr. I'd truly appreciate it if you can test it before I release a new version of knitr to CRAN.

devtools::install_github("yihui/knitr")

@jamieRowen
Copy link

jamieRowen commented Apr 30, 2018

I know this has been marked closed, but I get a similar persistence issue when using child option in chunks. The child document.Rmd will knit by itself, but get object not defined errors when knitting the parent.

Parent contains

```{python child='chapter4.Rmd'}

The confusing part is that this does not seem to be consistently true. For example the specific error I get refers to a chunk which has a reference to an object defined 6 chunks earlier. But the previous 5 chunk also refer to the same object, but give no such issue.

I have the dev version of knitr, rmarkdown and reticulate from github. Any ideas? Is this issue connected to the one closed here? Or is this a separate issue that should be opened?

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants