Search code examples
rstringhex

Converting "\x" to "\\x" for stri_unescape_unicode to read a string in R


I have a set of names that I need to convert from their current string form into their correct accents for graphics. One example is:

"\xf3"

I am trying to replace this string with:

"\\xf3"

So that when I run

stri_unescape_unicode("\\xf3")

I recieve:

"ó"

I have tried:

gsub("\x","\\x","\xf3")
Error: '\x' used without hex digits in character string starting ""\x"

However, gsub gets caught up in the hex digits and errors.


Solution

  • Well you do note need to replace anything, as there is no \ in the string \xf3. You can use cat('\xf3') and you will note that this is a multibyte string. You just need to change the encoding structure:

    In base R use:

    iconv("\xf3", "latin1")
    [1] "ó"
    

    If you insist on using stringi then:

    stringi::stri_unescape_unicode(encodeString("\xf3"))
    [1] "ó"
    

    To only Just changing "\xf3" to "\\xf3" you can do:

    `Encoding<-`("\xf3", "bytes") # ie multibyte
    [1] "\\xf3"
    
    encodeString("\xf3")
    [1] "\\xf3"