Stefan
Stefan

Reputation: 51

knitr fails to knit UTF-8 characters: "These lines contain invalid UTF-8 characters"

I try to knit a RMD file that contains UTF-8 characters.

The file looks as simple as this one:

test with some characters: ü ö ä ß

The command to knit looks like this

library(knitr)
options(encoding = "UTF-8")
knitr::knit2html("FILE_LOCATION/UTF file.Rmd",  output="Output_File.html")

it fails however: the file knits the text as "test with some characters: ü ö ä ß" and there is a warning:

The file .../UTF file.Rmd is not encoded in UTF-8. These lines contain invalid UTF-8 characters: 1

However, the file is UTF-8 (both where the command is executed from and the RMD file). I ensured that by RStudio, save with encoding, UTF-8 (which is anyways set as standard). Interestingly, if I use a slightly different command:

library(knitr)
options(encoding = "native.enc")       #  Only change in this line
knitr::knit2html("FILE_LOCATION/UTF file.Rmd",  output="Output_File.html")

One of my two computers knits the document correctly. The other, again, produces: "test with some characters: ü ö ä ß" without a warning. Strangely, both computers use Windows 10, the same version of R (R 3.5.3), RStudio (1.1.463) and knitr (tried both with the same result: 1.22 and 1.25).

Help appreciated.

Edit: added output of xfun::session_info("knitr")


> xfun::session_info("knitr")
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Locale:
  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    

Package version:
  evaluate_0.14   glue_1.3.0      graphics_3.5.1  grDevices_3.5.1 highr_0.8       knitr_1.25      magrittr_1.5   
  markdown_1.1    methods_3.5.1   mime_0.7        stats_3.5.1     stringi_1.2.4   stringr_1.3.1   tools_3.5.1    
  utils_3.5.1     xfun_0.10       yaml_2.2.0     

Upvotes: 4

Views: 6116

Answers (1)

Stefan
Stefan

Reputation: 51

SOLVED

Issue: knitting of special characters like “ö, ü, ß” in .RMD files (even though encoded in UTF-8) fails on Windows 10, markdown version => 1.0 and R version < 3.6.0.

Solution: update R.

Upvotes: 1

Related Questions