Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to share variables between knitr chunks using engine="bash"?

Tags:

knitr

Something like this, but it doesn't work:

```{r examples, engine="bash"}
    export EXAMPLES="example/path"
```

```{r example1, engine="bash"}
    echo $EXAMPLES
```

This works, however:

```{r examples2, engine="bash"}
    export EXAMPLES="example/path"
    echo $EXAMPLES
```
like image 298
nachocab Avatar asked Nov 10 '12 23:11

nachocab


1 Answers

That is a great question! It is also something I really want to accomplish but do not know how to do it (I appreciate if anybody can help me). It is not only useful for bash, but also for all other engines in knitr like python, ruby and so on. The reason that it does not work at the moment is knitr just runs the code via system('engine -arg code'), i.e. for each code chunk, a new engine session is opened, so all chunks are essentially executed in different processes.

Ideally I want an engine that opens a session and keeps on listening to new code, but I'm not sure if that is possible at all. AFAIK, the only way to share variables is to write them into files, which is obviously awkward.

like image 101
Yihui Xie Avatar answered Jan 03 '23 13:01

Yihui Xie