CaptainProg
CaptainProg

Reputation: 5690

Cross-OS character encoding issues causing difficulties in MATLAB and R

I use a Windows PC in the Office, a MacBook at home, and a Linux PC in the lab (I have my reasons in each case). I use Dropbox to synchronise my code across machines. Occasionally, I have cause to use a slightly unusual symbol in my code. For example, plotting a graph in R using a degree sign (°). If I save the file on the Mac and open it on the PC, the character has now rendered as '°'. If I change this back to the ° symbol on PC, when I return to the MacBook, it renders as a '?', and R refuses to run the code.

This is not exclusive to R; I have noticed this in MATLAB as well, often with similar characters. These just happen to be the two languages I do most of my work in. I understand that this probably has something to do with file encoding, but I have no idea how to make sure (at least for R and MATLAB) that I save in a compatible format. Avoiding the use of these characters is obviously one option, but is hardly ideal.

Is there a solution to this issue, either universally, or at least for R and MATLAB?

Upvotes: 1

Views: 345

Answers (1)

drammock
drammock

Reputation: 2543

This answer explains a lot about why getting UTF-8 to work with R on Windows is difficult (UTF-8 is default for R on Linux, and I think on Mac too). The safest and easiest path is probably to use unicode escape codes (like \u00B0 for the degree sign) in your scripts. That way your code will always be ASCII-compliant but the symbols should still render when plotting. The answers to this question should get you sorted for MATLAB (i.e., using feature('DefaultCharacterSet', 'UTF8') in your MATLAB startup file).

Upvotes: 1

Related Questions