RMarkdown:UTF-8与“编织"按钮配合使用,但不适用于render() [英] RMarkdown: UTF-8 works with Knit button but not with render()
问题描述
我正在RMarkdown中工作,试图呈现其中包含一些UTF-8字符的文档.当我按下RStudio中的编织"按钮时,一切正常.但是当我使用render()函数时,UTF-8丢失了.这是可复制代码的一小段:
I'm working in RMarkdown, trying to render a document that has some UTF-8 characters in it. When I push the "Knit" button in RStudio, everything works great. But when I use the render() function, the UTF-8 gets lost. Here's a short snippet of reproducible code:
---
output: html_document
---
Total nitrogen (µg/L)
Water temperature (°C)
无论我是在RStudio还是在Chrome中查看,按下编织"按钮都会为我提供正确的输出.但是,如果我使用render()渲染文件,则会得到:
Pushing the Knit button gives me the correct output, whether I view it in RStudio or in Chrome. But if I render the file with render(), I get:
总氮(µg/L)
Total nitrogen (µg/L)
水温(°C)
我正在Windows中工作,这可能是造成许多问题的根源.这是我的语言环境信息.
I'm working in Windows, which may be the source of much of the problem. Here's my locale info.
Sys.getlocale("LC_ALL")
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"
我尝试添加带有"options(encoding ='UTF-8')"的代码块,但这无济于事.我正在使用pwalk()自动生成具有不同参数的36个报告,因此我需要使用render()来完成此工作.
I've tried adding a code chunk with "options(encoding = 'UTF-8')" but it doesn't help. I'm using pwalk() to generate 36 reports automatically with different parameters, so I need to get this working with render().
推荐答案
您可以强制编码:
render("test.html",encoding="UTF-8")
您还可以使用R终端设置编码:
You can also set the encoding using you R terminal:
options(encoding = 'UTF-8')
render("test.html")
这篇关于RMarkdown:UTF-8与“编织"按钮配合使用,但不适用于render()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!