Reticulate - Running python chunks in Rmarkdown
You must use the Rstudio daily build (source) and upgrade knitr
, rmarkdown
to the latest version.
> packageVersion("rmarkdown")
[1] ‘1.9’
> packageVersion("knitr")
[1] ‘1.20’
Rmarkdown / knitr:
Running the chunks:
Running the chunks without knitting the document is not supported so far. See here: https://github.com/yihui/knitr/issues/1440 or Reticulate not sharing state between R/Python cells or Python/Python cells in RMarkdown.
Edit: Workaround by Freguglia:
"Workaround is to turn python chunks into R chunks and just wrap the whole content in the py_run_string() function, so whatever you assign in that piece of code is accessible from R by py$variable_name."
Knitting the document:
One way is to upgrade knitr
as suggested above, but you dont have to and you also dont need RStudio daily build.
If you have a version of knitr prior to 1.18, you can include:
```{r setup, include = FALSE}
knitr::knit_engines$set(python = reticulate::eng_python)
```
, see here: https://rstudio.github.io/reticulate/articles/r_markdown.html#engine-setup.
Python:
If it doesnt work ensure the python connection is running outside of rmarmdown/knitr:
py_run_string("x = 10"); py$x
.
In case that also doesnt work, you should check:
py_available()
and py_numpy_available()
.
If it returns FALSE
: Try to initialize it with: py_available(TRUE)
.
If that´s still a no - check your config:
py_config()
It will give you further hints on the problem:
Examples for me were: different bit versions of R and python (32 vs 64) or somehow i ran into trouble having installed both Python2.7 and seperately Anaconda.