在R jupyter笔记本中使用ipython magics?

els*_*ini 12 r jupyter-notebook jupyter-irkernel

我安装了jupyter conda install jupyter并且正在运行安装了r kernal的笔记本conda create -n my-r-env -c r r-essentials

我正在运行一个笔记本,并希望从shell运行bash命令.

!echo "hi"
Error in parse(text = x, srcfile = src): <text>:1:7: unexpected string constant
1: !echo "hi"
Run Code Online (Sandbox Code Playgroud)

为了比较,在带有python内核的笔记本中:

!echo "hi"
hi
Run Code Online (Sandbox Code Playgroud)

有没有办法设置R笔记本具有与ipython笔记本相同的功能关于bash命令(也许还有其他魔法)?

nat*_*lng 11

对于bash命令,可以使系统命令起作用.例如,在IRkernel中:

system("echo 'hi'", intern=TRUE)
Run Code Online (Sandbox Code Playgroud)

输出:

'hi'
Run Code Online (Sandbox Code Playgroud)

或者查看文件的前5行:

system("head -5 data/train.csv", intern=TRUE)
Run Code Online (Sandbox Code Playgroud)

由于IPython魔法在IPython内核中可用(但不在IRkernel中),我快速检查是否可以使用rPythonPythonInR库访问它们.但是,问题是get_ipython()Python代码不可见,因此以下都不起作用:

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')")
Run Code Online (Sandbox Code Playgroud)


joe*_*lom 5

system可以通过包装调用cat并指定为分隔符来对 的输出进行外观改进\'\\n\',该分隔符将输出显示在单独的行上,而不是用空格分隔(这是字符向量的默认设置)。这对于像 之类的命令非常有用tree,因为除非用换行符分隔,否则输出的格式没有什么意义。

\n\n

比较名为 的示例目录的以下内容test

\n\n

重击

\n\n
$ tree test\ntest\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 A1.tif\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 A2.tif\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 A3.tif\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 sample.R\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 sample2.R\n\n1 directory, 6 files\n
Run Code Online (Sandbox Code Playgroud)\n\n

Jupyter Notebook 中的 R

\n\n

system只是,难以理解的输出:

\n\n
> system(\'tree test\', intern=TRUE)\n\n\'test\' \'\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 A1.tif\' \'\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 A2.tif\' \'\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 A3.tif\' \'\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README\' \'\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\' \'    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 sample.R\' \'    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 sample2.R\' \'\' \'1 directory, 6 files\n
Run Code Online (Sandbox Code Playgroud)\n\n

cat+ system,输出类似于 bash:

\n\n
> cat(system(\'tree test\', intern=TRUE), sep=\'\\n\')\n\ntest\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 A1.tif\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 A2.tif\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 A3.tif\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 sample.R\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 sample2.R\n\n1 directory, 6 files\n
Run Code Online (Sandbox Code Playgroud)\n\n

请注意,对于像 之类的命令ls,上面会引入一个换行符,其中输出通常在 bash 中用空格分隔。

\n\n

您可以创建一个函数来节省一些输入:

\n\n
> # "jupyter shell" function\n> js <- function(shell_command){\n>     cat(system(shell_command, intern=TRUE), sep=\'\\n\')\n> }\n>\n> # brief syntax for shell commands\n> js(\'tree test\')\n
Run Code Online (Sandbox Code Playgroud)\n