Search code examples
r

Interpret escape sequences in a string


I have the following character vector in R:

test <- r"(\")"

> print(test)
[1] "\\\""

> cat(test)
\"

As you can see, it consists of two characters,

\"

that form an escape sequence for a single quote character:

"

How can I get the "interpreted" string, that consists only of the quote character, in R? In other words, I'm looking for a function that handles the string contained in test as if I typed it into the console manually.

Background: I get a malformed response from an API I have no control over. The response contains strings with escape sequences I'd like to get rid of. I could of course just replace the different sequences, but that seems not really clean.


Solution

  • You could use parse from base to interpret escape sequences in a string.

    s <- r"(A\u0042C)"
    
    as.character(parse(text=paste0("'", s, "'")))
    #[1] "ABC"
    
    stringi::stri_unescape_unicode(s)
    #[1] "ABC"
    

    In case you just want to remove \ you can use gsub what will fail in case there is something to interpret.

    s <- r"(\")"
    gsub("\\\\", "", s)
    #[1] "\""
    stringi::stri_unescape_unicode(s)
    #[1] "\""
    
    s <- r"(A\u0042C)"
    gsub("\\\\", "", s)
    #[1] "Au0042C"