Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R encoding unable to save symbol

I am working with an R script using RStudio (R version 2.15.3 on a PC [for various reasons I can't use a more updated version or R]) and am having trouble saving a script that has the parts per thousand symbol in it (‰). I can’t share my actual data but I have attached a simple example below:

library(ggplot2)
# Gen some random data
a <- data.frame(replicate(2,sample(1:10,500,rep=TRUE)))

# Plot with expressions for axes labels
basic <- ggplot(data = a, aes(X1, X2))+
geom_point()+
labs(list(colour="Catch Region", x=expression(paste(delta, ""^"13","C ","(‰)")), 
        y=expression(paste(delta, ""^"15","N ","(‰)"))))

basic

The graphed data in this example is nonsense but regardless it illustrates my point, as you can see my graph includes labels which have the ‰ symbol in them. Upon saving this script with RStudio I get the warring message:

Not all of the characters in C:/… could be encoded using ISO8859-1. To save using a different encoding, choose "File | Save with Encoding..." from the main menu.

RStudio has 12 different encoding types and I have tried them all, each either gives no warring upon saving but when the script is closed and reopened the ‰ symbol is gone, or gives the same warring as above with the following reload of the script producing nonsense characters instead of the ‰ symbol.

To date I have just been going through and changing the symbol back to ‰ each time I need to reopen the script, but it’s becoming a pain as my script library grows to have to do this for each one I open. Any help would be much appreciated.

Also, I realize there are a few other question on stackoverflow which deal with encoding issues but nothing I have found so far helps me with this specific issue.

like image 927
2ton21 Avatar asked Jun 18 '14 18:06

2ton21


People also ask

Which encoding should I save in R?

If in doubt about which encoding to use, use UTF-8, as it can encode any Unicode character.

What encoding does r use?

Character strings in R can be declared to be encoded in "latin1" or "UTF-8" or as "bytes" . These declarations can be read by Encoding , which will return a character vector of values "latin1" , "UTF-8" "bytes" or "unknown" , or set, when value is recycled as needed and other values are silently treated as "unknown" .


1 Answers

Use "File -> Save with encoding -> UTF-8". Unicode is a "superset" of all the encodings, thus it handles all the code points you can imagine. Moreover, R also is Unicode-aware.

like image 86
gagolews Avatar answered Oct 16 '22 10:10

gagolews