R Copy code
Sys.getlocale()
Sys.setlocale("LC_CTYPE", "en_US.UTF-8") 2. Check Your Terminal’s Encoding Ensure that the terminal where you're running R supports Unicode and is set to display characters correctly. For example:
In Linux, the terminal typically uses UTF-8 by default, but you may need to configure it manually. In Windows, make sure your console supports Unicode characters (you might want to use RStudio or Windows Terminal, which supports UTF-8 more effectively). In macOS, the default terminal also uses UTF-8. You can try setting the terminal's encoding to UTF-8 by running:
bash Copy code chcp 65001 # For Windows command prompt to set UTF-8 3. Printing Unicode Characters Directly If you're working with characters like ° (degree symbol), you can print them directly using their Unicode escape sequences. For example:
R Copy code
cat("\u00B0") # Prints the degree symbol (°) Or simply use the character:
R Copy code cat("The temperature is 25°C\n") # Prints "The temperature is 25°C" 4. Saving to Files with Correct Encoding If you're saving output to a file, ensure that the file is being written with the correct encoding. For example, when writing to a text file:
R Copy code writeLines("The temperature is 25°C", "output.txt", useBytes = TRUE) Make sure the file is saved with UTF-8 encoding, which most modern text editors support.
R Copy code charToRaw("\u00B0") This will show the raw byte sequence of the character.
By ensuring proper encoding both in R and in your terminal, you should be able to see the actual characters instead of their Unicode escape sequences.