yihui给出了对不同引擎使用cache选项的示例
yihui gives an example of using the cache option for the different engines
github/yihui/knitr-examples/blob/master/023-engine-python.Rmd
我似乎无法使其在python上正常工作.
I can't seem to get it to work for python.
以下作品
```{r,engine='python',cache=TRUE} x=10 print x ```但这不起作用
```{r,engine='python',cache=TRUE} x = 10 ``` ```{r,engine='python',cache=TRUE} print x ```有人有主意吗?
推荐答案块选项cache不会保存在块中为R以外的语言定义的所有变量.但是,这是节省打印输出的结果,因此,如果您计算需要花费一些时间的内容,则无需重新计算任何结果.在knitr网站上:
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:
除engine='R'(默认值)外,所有块均在单独的位置执行 会话,因此无法直接共享变量.如果我们想 利用先前块中创建的对象,我们通常必须 将它们写入文件(作为副作用).对于bash引擎,我们可以使用 Sys.setenv()将变量从R导出到bash(示例).
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).
有可能在shell的环境中保存一些值,并通过读取环境从其他单元格中检索这些值.这是 Yihui接受的Polyglot示例.因此,对于Python,如果您可以将值格式化为字符串并将其传递给sys.setenv(),则可以通过调用sys.getenv()在另一个单元格(作为单独的Python会话运行)中使用该值.
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().
尽管如此,我对C和Fortran引擎采用的方法感到有些困惑.那些似乎可以通过一些称为.C() 的函数或函数称为.Fortran() .但是,似乎Python没有等效功能.
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.
更多推荐
knitr
发布评论