bookdown 项目中的 Rmd 文件名中包含这种类型的字符 Naïve-Bayes.Rmd
,使用 bookdown::render_book('index.Rmd')
会报这种如下错误
Error in check_special_chars(files) :
Filenames must not contain special characters
Calls: <Anonymous> ... setdiff -> as.vector -> source_files -> check_special_chars
In addition: Warning message:
In check_special_chars(files) :
The filename "05-Na?ve-Bayes.Rmd" contains special characters. You may rename it to, e.g., "05-Na-ve-Bayes.Rmd".
Execution halted
Exited with status 1.
根据错误提示,我运行
bookdown:::check_special_chars('Naïve-Bayes.Rmd')
[1] "Naïve-Bayes.Rmd"
结果是没问题,继续查 bookdown:::check_special_chars
function (filename)
{
reg = rmarkdown:::.shell_chars_regex
for (i in grep(reg, filename)) warning("The filename \"",
filename[i], "\" contains special characters. ",
"You may rename it to, e.g., \"", gsub(reg, "-",
filename[i]), "\".")
if (!is.null(i))
stop("Filenames must not contain special characters")
filename
}
<bytecode: 0x0000005a61fe7c30>
<environment: namespace:bookdown>
继续查 rmarkdown:::.shell_chars_regex
rmarkdown:::.shell_chars_regex
[1] "[ <>()|\\:&;#?*']"
没说不允许这种字符,继续查,不使用 bookdown::render_book
而使用 rmarkdown::render
rmarkdown::render('05-Naïve-Bayes.Rmd')
processing file: 05-Na<U+00EF>ve-Bayes.Rmd
|.................................................................| 100%
ordinary text without R code
output file: 05-Na<U+00EF>ve-Bayes.knit.md
"C:/PROGRA~3/CHOCOL~1/bin/pandoc" +RTS -K512m -RTS 05-Naïve-Bayes.utf8.md --to html4 --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash+smart --output 05-Naïve-Bayes.html --email-obfuscation none --wrap preserve --standalone --section-divs --table-of-contents --toc-depth 3 --template "D:\library\bookdown\templates\gitbook.html" --highlight-style pygments --number-sections --css style.css --include-in-header "C:\Users\xy-huang\AppData\Local\Temp\RtmpOOXf8D\rmarkdown-strd545e286996.html" --mathjax --metadata pagetitle=05-Naïve-Bayes.utf8.md
Output created: final-words.html
自然 knitr::knit
也不会有毛病
knitr::knit('05-Naïve-Bayes.Rmd')
processing file: 05-Na<U+00EF>ve-Bayes.Rmd
|.................................................................| 100%
ordinary text without R code
output file: 05-Na<U+00EF>ve-Bayes.md
[1] "05-Naïve-Bayes.md"
看起来没毛病,首先我认为 Naïve-Bayes.Rmd
文件名应该被允许,不然那些德文该怎么破,从上面的追查结果看也是允许这种文件名的,那么问题来了,这报错来自bookdown的哪里?怀疑可能是某个隐藏颇深的编码转化问题?
下附环境信息
xfun::session_info(c('rmarkdown','bookdown'))
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600), RStudio 1.2.1335
Locale:
LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese (Simplified)_China.936
LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C
LC_TIME=Chinese (Simplified)_China.936
Package version:
base64enc_0.1.3 bookdown_0.11 digest_0.6.19 evaluate_0.14 glue_1.3.1 graphics_3.6.0
grDevices_3.6.0 highr_0.8 htmltools_0.3.6 jsonlite_1.6 knitr_1.23 magrittr_1.5
markdown_1.0 methods_3.6.0 mime_0.6 Rcpp_1.0.1 rmarkdown_1.13 stats_3.6.0
stringi_1.4.3 stringr_1.4.0 tinytex_0.13 tools_3.6.0 utils_3.6.0 xfun_0.7
yaml_2.2.0
Pandoc version: 2.7.2
重现本问题,只需执行以下三步
- 打开 RStudio 新建 bookdown 项目
- 将其中某个 Rmd 文件如
05-application.Rmd
重命名为 05-Naïve-Bayes.Rmd
- 运行
bookdown::render_book('index.Rmd')
即重现问题
traceback()
定位到 check_special_chars
bookdown::render_book('index.Rmd')
Error in check_special_chars(files) :
Filenames must not contain special characters
In addition: Warning message:
In check_special_chars(files) :
The filename "05-Na?ve-Bayes.Rmd" contains special characters. You may rename it to, e.g., "05-Na-ve-Bayes.Rmd".
> traceback()
6: stop("Filenames must not contain special characters")
5: check_special_chars(files)
4: source_files(format, config)
3: as.vector(x)
2: setdiff(source_files(format, config), main)
1: bookdown::render_book("index.Rmd")
问题可能是把文件名中的字符给转换坏了,比如转成问号 ?