gpt4 book ai didi

r - 在 R jupyter 笔记本中使用 ipython 魔法?

转载 作者:行者123 更新时间:2023-12-01 19:55:02 26 4
gpt4 key购买 nike

我使用 conda install jupyter 安装了 jupyter,并且正在运行一个笔记本,其中安装了从 conda create -n my-r-env -c r r-essentials 安装的 r 内核

我正在运行笔记本并希望从 shell 运行 bash 命令。

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

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

!echo "hi"
hi

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

最佳答案

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

system("echo 'hi'", intern=TRUE)

输出:

'hi'

或者查看文件的前 5 行:

system("head -5 data/train.csv", intern=TRUE)

由于 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')")

关于r - 在 R jupyter 笔记本中使用 ipython 魔法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35298513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com