Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

r - knitr - Python engine cache option not working

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...