在R jupyter笔记本中使用ipython魔术吗? [英] Using ipython magics in R jupyter notebook?
问题描述
我用conda install jupyter
安装了jupyter,并且正在运行从conda create -n my-r-env -c r r-essentials
I installed jupyter with conda install jupyter
and am running a notebook with the r kernal installed from conda create -n my-r-env -c r r-essentials
我正在运行笔记本,并且想从外壳运行bash命令.
I am running a notebook and want to run a bash command from a shell.
!echo "hi"
Error in parse(text = x, srcfile = src): <text>:1:7: unexpected string constant
1: !echo "hi"
为进行比较,在带有python内核的笔记本中:
For comparison, in an notebook with a python kernel:
!echo "hi"
hi
就bash命令(也许还有其他魔术)而言,是否有办法将R笔记本设置为具有与ipython笔记本相同的功能?
Is there a way to set up R notebooks to have the same functionality as the ipython notebook with regards to bash commands (and maybe other magics)?
推荐答案
仅对bash命令而言,可以使系统命令正常工作.例如,在IRkernel中:
For just bash commands, it's possible to get system commands to work. For example, in the IRkernel:
system("echo 'hi'", intern=TRUE)
输出:
'hi'
或者查看文件的前5行:
Or to see the first 5 lines of a file:
system("head -5 data/train.csv", intern=TRUE)
由于IPython魔术在IPython内核中可用(但在IRkernel中不可用),因此我快速检查了是否可以使用rPython
和PythonInR
库访问它们.但是,问题在于get_ipython()
对Python代码不可见,因此以下任何一项均无效:
As IPython magics are available in the IPython kernel (but not in the IRkernel), I did a quick check if it was possible to access these using the rPython
and PythonInR
libraries. However, the issue is that get_ipython()
isn't visible to the Python code, so none of the following worked:
library("rPython")
rPython::python.exec("from IPython import get_ipython; get_ipython().run_cell_magic('writefile', 'test.txt', 'This is a test')")
library("PythonInR")
PythonInR::pyExec("from IPython import get_ipython; get_ipython().run_cell_magic('head -5 data/test.csv')")
这篇关于在R jupyter笔记本中使用ipython魔术吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!