knitr - Python engine cache option not working

北慕城南 提交于 2019-11-28 04:42:29

问题


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

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

I can't seem to get it to work for python.

The following works

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

But this doesn't work

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

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

Anyone have an idea?


回答1:


The chunk option cache doesn't save all the variables defined in the block for languages other than R. It is, though, saving printed outputs, so if you compute something that takes a while, any results will not need to be re-computed. From the knitr website:

Except engine='R' (default), all chunks are executed in separate sessions, so the variables cannot be directly shared. If we want to make use of objects created in previous chunks, we usually have 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).

It's possible to save a few values in the shell's environment, and retrieve those values from the other cells by reading the environment. This is the approach Yihui took in the Polyglot example. So, for Python, if you can format the value as a string and pass it to sys.setenv(), you could use that value in another cell (run as a separate Python session) by calling sys.getenv().

Though, I am mildly confused about the approach taken with the C and Fortran engines. Those seem to have access to compiled functions in later chunks by using some function called .C() or a function called .Fortran(). But it seems that Python does not have an equivalent.



来源:https://stackoverflow.com/questions/30175948/knitr-python-engine-cache-option-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!