Search code examples
rrstudio

RStudio: keeping special characters in a script


I wrote a script with German special characters e.g. ü. However, whenever I close R and reopen the script the characters are substituted:

Before "für"; "hinzufügen"; "Ø" - After "für"; "hinzufügen"; "Ã".

I tried to remedy it using save with encoding and choosing UTF-8 as it is stated here but it did not work.

What am I missing?


Solution

  • You don't say what OS you're using, but this kind of thing really only happens on Windows nowadays, so I'll assume that.

    The problem is that Windows has a local encoding that is not UTF-8. It is commonly something like Latin1 in English-speaking countries. I'm not sure what encoding people use in German-speaking countries, if that's where you are. From the junk you saw, it looks as though you saved the file in UTF-8, then read it using your local encoding. The encodings for writing and reading have to match if you want things to work.

    In RStudio you can try "Reopen with encoding..." and specify UTF-8, and you'll probably get your original back, as long as you haven't saved it after the bad read. If you did that, you've got a much harder cleanup to do.