问题描述
我有一个Jupyter笔记本(python3)这是一个批处理作业 - 它使用%run
运行三个单独的python3笔记本。我想从我的批处理中调用第四个Jupyter R-kernel笔记本。
I have a Jupyter notebook (python3) which is a batch job -- it runs three separate python3 notebooks using %run
. I want to invoke a fourth Jupyter R-kernel notebook from my batch.
有没有办法在Jupyter中用Python笔记本执行外部R笔记本iPython?
当前设置:
run_all.ipynb
:( python3内核)
run_all.ipynb
: (python3 kernel)
%run '1_py3.ipynb'
%run '2_py3.ipynb'
%run '3_py3.ipynb'
%run '4_R.ipynb'
三个python3笔记本正确运行。 R笔记本在Jupyter中单独打开时运行正常 - 但是当从 run_all.ipynb
使用%run
调用时它会失败。它被解释为python,并且单元格在第一行出现python错误:
The three python3 notebooks run correctly. The R notebook runs correctly when opened separately in Jupyter -- however it fails when called using %run
from run_all.ipynb
. It is interpreted as python, and the cell gives a python error on the first line:
cacheDir <- "caches"
TypeError:一元的坏操作数类型 - :'str'
TypeError: bad operand type for unary -: 'str'
我对从python笔记本运行单独的R笔记本的任何解决方案感兴趣 - Jupyter magic,shell,python库等等。我也对一个解决方法感兴趣 - 例如一个方法(比如shell脚本)可以运行所有四个笔记本(python3和R),即使这不能在python3笔记本中完成。
I am interested in any solution for running a separate R notebook from a python notebook -- Jupyter magic, shell, python library, et cetera. I would also be interested in a workaround -- e.g. a method (like a shell script) that would run all four notebooks (both python3 and R) even if this can't be done from inside a python3 notebook.
(注意:我已经了解如何在单元格中嵌入 %% R
。这不是我的意思我想打电话给一个完全独立的R笔记本。)
(NOTE: I already understand how to embed %%R
in a cell. This is not what I am trying to do. I want to call a complete separate R notebook.)
推荐答案
我认为你不能使用%run
magic命令,当它在当前内核中执行文件时。
I don't think you can use the %run
magic command that way as it executes the file in the current kernel.
Nbconvert有一个执行API允许你执行笔记本。所以你可以创建一个执行所有笔记本的shell脚本,如下所示:
Nbconvert has an execution API that allows you to execute notebooks. So you could create a shell script that executes all your notebooks like so:
#!/bin/bash
jupyter nbconvert --to notebook --execute 1_py3.ipynb
jupyter nbconvert --to notebook --execute 2_py3.ipynb
jupyter nbconvert --to notebook --execute 3_py3.ipynb
jupyter nbconvert --to notebook --execute 4_R.ipynb
由于您的笔记本电脑不需要共享状态,这应该没问题。或者,如果您真的想在笔记本中使用它,可以使用execute Python API从笔记本中调用nbconvert。
Since your notebooks require no shared state this should be fine. Alternatively, if you really wanna do it in a notebook, you use the execute Python API to call nbconvert from your notebook.
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
with open("1_py3.ipynb") as f1, open("2_py3.ipynb") as f2, open("3_py3.ipynb") as f3, open("4_R.ipynb") as f4:
nb1 = nbformat.read(f1, as_version=4)
nb2 = nbformat.read(f2, as_version=4)
nb3 = nbformat.read(f3, as_version=4)
nb4 = nbformat.read(f4, as_version=4)
ep_python = ExecutePreprocessor(timeout=600, kernel_name='python3')
#Use jupyter kernelspec list to find out what the kernel is called on your system
ep_R = ExecutePreprocessor(timeout=600, kernel_name='ir')
# path specifies which folder to execute the notebooks in, so set it to the one that you need so your file path references are correct
ep_python.preprocess(nb1, {'metadata': {'path': 'notebooks/'}})
ep_python.preprocess(nb2, {'metadata': {'path': 'notebooks/'}})
ep_python.preprocess(nb3, {'metadata': {'path': 'notebooks/'}})
ep_R.preprocess(nb4, {'metadata': {'path': 'notebooks/'}})
with open("1_py3.ipynb", "wt") as f1, open("2_py3.ipynb", "wt") as f2, open("3_py3.ipynb", "wt") as f3, open("4_R.ipynb", "wt") as f4:
nbformat.write(nb1, f1)
nbformat.write(nb2, f2)
nbformat.write(nb3, f3)
nbformat.write(nb4, f4)
请注意,这只是从nbconvert执行API文档中复制的示例:
Note that this is pretty much just the example copied from the nbconvert execute API docs: link
这篇关于Jupyter可以在Python笔记本中运行单独的R笔记本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!