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

12,446

Solution 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")

Solution 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"

Share:
12,446
dnidz
Author by

dnidz

I'm an ecosystem ecologist with a flair for urban systems, figuring out how the green and grey pieces interact.

Updated on June 26, 2022

Comments

  • dnidz
    dnidz almost 2 years

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

  • dnidz
    dnidz over 6 years
    Forcing the encoding within the render() works great -- thank you! However, setting the encoding in the R terminal and then calling render() doesn't work.
  • dnidz
    dnidz over 6 years
    From a fresh RStudio session I still get the same behavior -- the file will render properly with the Knit GUI button, but not by calling render(). Setting the encoding before the render statement doesn't work, either, even though the encoding does successfully change. The advice from agstudy does work to specify encoding within the render() call: render("test.Rmd",encoding="UTF-8")