How to source() .R file saved using UTF-8 encoding?

On R/Windows, source runs into problems with any UTF-8 characters that can’t be represented in the current locale (or ANSI Code Page in Windows-speak). And unfortunately Windows doesn’t have UTF-8 available as an ANSI code page–Windows has a technical limitation that ANSI code pages can only be one- or two-byte-per-character encodings, not variable-byte encodings like UTF-8.

This doesn’t seem to be a fundamental, unsolvable problem–there’s just something wrong with the source function. You can get 90% of the way there by doing this instead:

eval(parse(filename, encoding="UTF-8"))

This’ll work almost exactly like source() with default arguments, but won’t let you do echo=T, eval.print=T, etc.

Leave a Comment