技术咨询:Quarto能否渲染包含IPython魔法命令的文档?
Quarto渲染IPython魔法命令相关问题解答
Quarto支持渲染包含IPython魔法命令的文档,但你遇到的错误是因为当前配置的引擎不匹配:
- 你的文档指定了
jupyter: r-reticulate,这个是基于R的reticulate引擎,并非标准IPython内核,所以无法识别IPython的%load_ext魔法命令。 - 若要使用
rpy2(Python调用R),需修改文档头部的配置:
将yaml中的jupyter: r-reticulate替换为jupyter: python3(或直接删除该配置,Quarto会默认使用Python内核),之后Python代码块中的%load_ext rpy2.ipython即可正常运行,后续还能通过%%R魔法命令编写R代码。示例:--- title: "Untitled" format: pdf jupyter: python3 ---%load_ext rpy2.ipython %%R # 这里写R代码 print("Hello from R") - 若你是想在R环境中调用Python(依赖reticulate),则应使用R代码块,通过reticulate的原生函数实现,而非IPython魔法命令。示例:
--- title: "Untitled" format: pdf jupyter: r-reticulate ---library(reticulate) use_python("/path/to/python") py_run_string("print('Hello from Python')")
内容的提问来源于stack exchange,提问作者PaulS
相关产品推荐
相关产品推荐

