Knitr - Python caching option not working

yihui gives an example of using the cache option for different engines

https://github.com/yihui/knitr-examples/blob/master/023-engine-python.Rmd

I can't get it to work in python.

Next works

```{r,engine='python',cache=TRUE} x=10 print x ``` 

But it does not work

 ```{r,engine='python',cache=TRUE} x = 10 ``` ```{r,engine='python',cache=TRUE} print x ``` 

Anyone have an idea?

+4
python r reproducible-research knitr
source share
1 answer

The chunk cache parameter does not save all variables defined in the block for languages ​​other than R This, however, is the preservation of printed outputs, so if you are calculating something that takes some time, any results do not need to be re-read. On the knitr website:

With the exception of engine='R' (default), all chunks are executed in separate sessions, so variables cannot be directly separated. If we want to use objects created in previous pieces, we usually need to write them to files (as side effects). For the bash engine, we can use Sys.setenv() to export variables from R to bash (example).

You can save multiple values ​​in a shell environment and get these values ​​from other cells by reading the environment. This approach Yihui took an example of Polyglot . So, for Python, if you can format the value as a string and pass it to Sys.setenv() , you can use that value in another cell (executed as a separate Python session) by calling sys.getenv() .

Although, I was a bit confused about the approach taken with the C and Fortran engines. It seems that they have access to compiled functions in later snippets using some function called .C() or called .Fortran() . But it looks like Python has no equivalent.

+4
source share

All Articles