[R] Set encoding when load()-ing workspaces?

Duncan Murdoch murdoch.duncan at gmail.com
Sun May 2 18:30:33 CEST 2010


Gustaf Rydevik wrote:
> Hi all,
>
> I hope that there is someone that can help me out here.
> I am trying to load() a workspace on os x (R 2.11.0) that was saved in
> windows XP (R 2.9). In that workspace, there's a data.frame with names
> that contain swedish characters. These characters become garbled,
> which is a major problem.
> >From the R windows FAQ, I read:
>
> "Note though that character data in a workspace will be in a
> particular encoding that is not recorded in the workspace, so
> workspaces containing non-ASCII character data may not be
> interchangeable even on the same OS. Since R marks character data when
> it knows it to be in UTF-8 or Latin-1 (including its Windows superset,
> CP1252), strings in those encodings are likely to be transferred
> correctly: fortunately this covers most of the common cases (Mac OS X
> normally uses UTF-8, and Linux users are likely to use UTF-8 or
> perhaps Latin-1 (which used to be used for English)). "
>
> Apparently, my case is not the most common one, and I don't know why.
> I've been trying to dig into the load() function, but since it uses a
> lot of .Internal functions, I get stuck there.
> I've also tried doing options(encoding="latin1"), which doesn't seem
> to change anything.
>   

You can't change the encoding when you load, but you can convert the 
encoding later (using iconv()) if you know what encoding it is.  A good 
guess for a file created on Windows in my locale is "latin1", but it's 
not certain, and I don't know what is commonly used on Windows in a 
Swedish locale.

If you have an example where you know the correct version of the string 
and you can show us what you're getting, together with charToRaw() 
applied to it, someone will probably be able to make a guess at the 
encoding.

Duncan Murdoch


> And now I'm stuck. Any suggestions on where to look?
> I've run into this issue twice before. The first time I managed to get
> it solved, but can't remember how (perhaps a .Rprofile setting
> somewhere?).
> The second time, I mailed R-Sig-Mac, got some tips that unfortunately
> did not lead anywhere, and subsequently gave up. I hope third time's a
> charm!
>
> Many thanks in advance,
> Gustaf
>
>
>



More information about the R-help mailing list