无法编译Rmarkdown:Pandoc编码错误求助及解决方案咨询
Rmarkdown编译test.Rmd报错的原因分析与解决方法
首次使用Rmarkdown编译自带的test.Rmd文件时,出现以下编码相关错误:
pandoc.exe: <stdout>: commitBuffer: invalid argument (invalid character) pandoc.exe: <stdout>: commitBuffer: invalid argument (cannot encode character '\7853') |................. | 29% [setup] processing file: test.Rmd output file: test.knit.md "C:/PROGRA~1/Pandoc/pandoc" +RTS -K512m -RTS test.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output test.html --lua-filter "C:\Users\Thuận\AppData\Local\R\win-library\4.3\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\Thuận\AppData\Local\R\win-library\4.3\rmarkdown\rmarkdown\lua\latex-div.lua" --embed-resources --standalone --variable bs3=TRUE --section-divs --template "C:\Users\Thuận\AppData\Local\R\win-library\4.3\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable theme=bootstrap --mathjax --variable "mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" --include-in-header "C:\Users\THUN~1\AppData\Local\Temp\Rtmp6PJX5N\rmarkdown-str59dc75265874.html" pandoc.exe: recoverEncode: invalid argument (cannot encode character '\7853') Error: pandoc document conversion failed with error 1 In addition: Warning messages: 1: In system(paste(shQuote(path), "--version"), intern = TRUE) : running command '"C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/pandoc.exe" --version' had status 1 2: In system(paste(shQuote(path), "--version"), intern = TRUE) : running command '"C:/PROGRA~1/Pandoc/pandoc.exe" --version' had status 1 Execution halted
错误原因
核心问题是系统路径中包含非ASCII字符(比如用户文件夹名称Thuận带有越南语特殊字符),导致pandoc无法正确编码解析路径和相关文件,同时引发pandoc版本检测失败的警告。
解决方法
方法一:修改R包安装路径到纯ASCII路径
- 打开RStudio,执行代码设置新的纯ASCII路径作为包安装目录(示例路径为
C:/R/library,可自行调整):.libPaths("C:/R/library") - 重新安装rmarkdown及相关依赖:
install.packages("rmarkdown") - 重新尝试编译test.Rmd文件。
方法二:更换Windows用户到纯ASCII用户名
- 创建新的Windows用户,用户名仅使用英文/ASCII字符。
- 切换到新用户环境,重新安装R、RStudio及所需包。
- 在新环境下编译Rmarkdown文件。
方法三:临时设置系统编码(应急方案)
编译前在RStudio中执行以下代码,临时切换系统编码:
Sys.setlocale(category = "LC_ALL", locale = "English_US.UTF-8")
执行后再尝试编译,此方法仅为临时解决方案,长期建议优先选择前两种方法。
内容的提问来源于stack exchange,提问作者Enzo
相关产品推荐
相关产品推荐

