RMarkdown: UTF-8 works with Knit button but not with render()

我的梦境 提交于 2019-12-10 17:29:09

问题


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)

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:

Total nitrogen (µg/L)

Water temperature (°C)

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"

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().


回答1:


You can force encoding:

render("test.html",encoding="UTF-8")

You can also set the encoding using you R terminal:

options(encoding = 'UTF-8')
render("test.html")



回答2:


I was considering this as a comment because it does not necessarily answer your question, however, it is too long to get lost there.

Firstly, using the knit button in RStudio does call render, so all things being equal, both running from the console and via the GUI will produce the same output.

your environment

An important note from jjallaire in an old closed issue on Github:

when RStudio calls render it is in a fresh R process rather than in the global environment of the current session (which is what you would git when calling render at the console)

A good question that provides context is here.

initial conclusion

If the document renders correctly using the GUI button and not from the console, then there is something in your environment causing the encoding to be read incorrectly.

Try from a clean session, if it still produces the same output then that would suggest an issue in the the environment at startup. Check the encoding...

getOption("encoding")
# [1] "native.enc"

Instead of placing options(encoding = "UTF-8") in a code chunk, execute it before your render statement. You can check that it has changed by running the getOption as above again and confirm it now returns # [1] "UTF-8"



来源:https://stackoverflow.com/questions/47402718/rmarkdown-utf-8-works-with-knit-button-but-not-with-render

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!