在RStudio中渲染含R与Python的Quarto PDF文件时遇报错
Quarto渲染R+Python混合文档为PDF时的列数匹配错误
错误现象
渲染包含R和Python代码的Quarto文件为PDF时,持续抛出以下错误:
Error in rbind(info, getNamespaceInfo(env, "S3methods")) : number of columns of matrices must match (see arg 2) Calls: :: ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous> Execution halted
补充说明
- 错误信息未指向具体代码块
- 单独运行所有代码块均无报错,仅点击渲染按钮时触发错误
- 已安装并加载
rmarkdown和knitr包
完整错误信息
Error in rbind(info, getNamespaceInfo(env, "S3methods")) : number of columns of matrices must match (see arg 2) Calls: :: ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous> Execution halted R installation: Version: 3.5.1 Path: C:/Users/marie/anaconda3/envs/myenviro310/lib/R LibPaths: - C:/Users/marie/AppData/Local/R/win-library/4.4 - C:/Users/marie/anaconda3/envs/myenviro310/Lib/R/library knitr: (None) rmarkdown: (None) The knitr package is not available in this R installation. Install with install.packages("knitr") The rmarkdown package is not available in this R installation. Install with install.packages("rmarkdown")
当前会话信息
R version 4.4.1 (2024-06-14 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows 10 x64 (build 19045) Matrix products: default locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 [3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C [5] LC_TIME=English_United States.utf8 time zone: America/New_York tzcode source: internal attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] reticulate_1.39.0 png_0.1-8 reshape2_1.4.4 lubridate_1.9.3 forcats_1.0.0 [6] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 [11] tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0 loaded via a namespace (and not attached): [1] Matrix_1.7-0 gtable_0.3.5 jsonlite_1.8.8 compiler_4.4.1 tidyselect_1.2.1 [6] Rcpp_1.0.13 scales_1.3.0 lattice_0.22-6 plyr_1.8.9 R6_2.5.1 [11] labeling_0.4.3 generics_0.1.3 knitr_1.48 munsell_0.5.1 pillar_1.9.0 [16] tzdb_0.4.0 rlang_1.1.4 utf8_1.2.4 stringi_1.8.4 xfun_0.47 [21] timechange_0.3.0 cli_3.6.3 withr_3.0.1 magrittr_2.0.3 grid_4.4.1 [26] rstudioapi_0.16.0 hms_1.1.3 lifecycle_1.0.4 vctrs_0.6.5 glue_1.7.0 [31] farver_2.1.2 fansi_1.0.6 colorspace_2.1-1 tools_4.4.1 pkgconfig_2.0.3
已尝试的无效操作
- 检查代码格式错误
- 重写代码排查问题
- 通过ChatGPT寻找解决方案
解决方案建议
1. 统一Quarto调用的R版本
从完整错误信息可知,Quarto渲染时使用的是conda环境myenviro310中的R 3.5.1,但当前会话使用的是R 4.4.1,版本不一致且旧版R缺少knitr和rmarkdown包。可通过以下方式解决:
- 在Quarto文件的YAML头部指定正确的R路径:
format: pdf execute: r: path: "C:/Users/marie/AppData/Local/Programs/R/R-4.4.1/bin/R.exe" - 或更新conda环境中的R版本并安装所需包:
conda activate myenviro310 conda install r-base=4.4.1 R -e "install.packages(c('knitr', 'rmarkdown'))"
2. 排查包兼容性问题
会话中加载了reticulate用于Python交互,可能存在版本冲突:
- 更新所有R包至最新版本:
update.packages(ask = FALSE) - 临时移除
reticulate包的加载,测试是否能正常渲染,确认是否为该包导致的冲突。
3. 清理渲染缓存
缓存文件可能引发异常,清理后重新渲染:
- 删除Quarto文件所在目录下的
_quarto_cache文件夹 - 重新点击渲染按钮
内容的提问来源于stack exchange,提问作者user27258011
相关产品推荐
相关产品推荐

