这是我在 SO 上的第一篇文章,所以请告诉我我是否做错了什么。
我对在 ipython 中使用不同的编程语言感兴趣,类似于 emacs org 模式下的 babel/literal 编程。我喜欢 emacs org 模式的一点是,可以有多个“单元”连接到同一个 R/bash 会话。这允许我重复使用在文档前面部分创建的变量/函数,即使我在中间做了其他事情。
我发现这在 ipython 和 Rmagic 中是可能的。举个例子
In [1]: %load_ext rpy2.ipython
In [2]: %%R
a <- 3
a
Out [2]: 3
In [3]: something_in_python = 'I am doing something unrelated now'
In [4]: %%R
cat('My variable a is still here, its value is: ', a) # a is still here!
Out [4]: My variable is still here, its value is: 3
我非常希望能够在 bash 中做类似的事情。但是,无论我使用“%%script bash”还是%%sx,变量都不是持久的。这是我正在尝试做的事情:
In [1]: %%script bash
var1="hello"
echo $var1
Out[1]: hello
In [2]: %%script bash
echo $var1 # I need $var1 to be present in this cell too - but its gone!
Out[62]:
是否可以在多个单元中拥有相同的基本会话?或者至少以某种方式传递变量。当然,我可以尝试将变量传递到 python 中,然后返回到下一个 bash 单元,但我有一种感觉,一定有更好的方法。感谢您的帮助!
PS:我寻找解决方案,但我在这里或通过谷歌搜索都没有找到任何东西。有一些这样的东西:IPython Notebook 之前的单元格内容,但这似乎对我的情况没有帮助。