简体   繁体   English

如何处理Knitr spin_child中的编码

[英]How to handle encodings in knitr spin_child

Following recommendations on https://yihui.name/en/2018/11/biggest-regret-knitr , I started saving my .R files with UTF-8 encoding (using RStudios "Save with encoding"). 按照https://yihui.name/zh/2018/11/biggest-regret-knitr上的建议,我开始使用UTF-8编码(使用RStudios“使用编码保存”)保存.R文件。 Seems to work well, until it comes to using spin_child. 在使用spin_child之前,它似乎工作良好。

Under Windows, for example I have a file mainfile.R with this code: 例如,在Windows下,我有一个文件mainfile.R ,其代码如下:

print("Bär 1"); spin_child("subfile.R") 

subfile.R has this code: subfile.R具有以下代码:

print("Bär 2")

In RStudio under Windows, I run 在Windows下的RStudio中,我运行

rmarkdown::render("mainfile.R", encoding = "UTF-8")

If both files are saved with encoding UTF-8, Bär 1 comes out fine, but Bär 2 comes out as "Bär 2" (which seems like an encoding problem to me). 如果两个文件都使用UTF-8编码保存,则Bär1会很好,但Bär2会以“BÃr2”的形式出现(这对我来说似乎是编码问题)。 Interestingly, if I change encoding of subfile.R to ISO-8859-1 (System default), both Bär come out correctly. 有趣的是,如果我将subfile.R的编码更改为ISO-8859-1(系统默认值),则两个Bär都会正确显示。 It seems strange to me that I should have to use different encodings for different files, so I'm wonderning what I have to do if the input file for spin_child ist UTF-8 as well? 我对不同的文件必须使用不同的编码对我来说似乎很奇怪,所以我想知道如果spin_child ist UTF-8的输入文件也要做什么?

This is a bug in knitr , and I just fixed it on Github . 这是knitr中的错误,我刚刚在Github上进行修复 Please try the development version and continue using UTF-8: 请尝试开发版本并继续使用UTF-8:

remotes::install_github('yihui/knitr')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM