Title: Replace a UTF-8 character in R
Tags: r;utf-8
Question: I have an R "plugin" that reads a bunch of lines from stdin, parses it and evaluates it.

```...
code <- readLines(f, warn=F)   ## that's where the lines come from...
result <- eval(parse(text=code))
...
```

Now, sometimes the system that provides the lines of code kindly inserts a UTF-8 non-break space (```U+00A0``` = ```\xc2\xa0```) here and there in the code. The ```parse()``` chokes on such characters. Example:

```s <- "1 +\xc2\xa03"
s
[1] "1 + 3"   ## looks fine doesn't it? In fact, the Unicode "NON-BREAK SPACE" is there

eval(parse(text=s))
Error in parse(text = s) : <text&gt;:1:4: unexpected input
1: 1 +?
      ^

eval(parse(text=gsub("\xc2\xa0"," ",s)))
[1] 4
```

I would like to replace that character with a regular space, and can do so (but at my own peril, I guess) as above with this:

```code <- gsub('\xc2\xa0',' ',code)
```

However, this is not clean as the byte sequence ```'\xc2\a0'``` could conceivably start matching in the middle of another 2-byte char whose 2nd byte is ```0xc2```.

Perhaps a bit better, we can say:

```code <- gsub(intToUtf8(0x00a0L),' ',code)
```

But this would not generalize to a UTF-8 string.

Surely there is a better, more expressive way to enter a string containing some UTF-8 characters?  In general, what's the right way to express a UTF-8 string (here, the pattern argument of ```sub()```)?



Edit: to be clear, I am interested in entering UTF-8 chars in a String by specifying their hexadecimal value. Consider the following example (note that ```"é"``` is Unicode ```U+00E9``` and can be expressed in UTF-8 as ```0xc3a9```):

```s <- "Cet été."
gsub("té","__",s)
# --&gt; "Cet é__."
# works, but I like to keep my code itself free of UTF-8 literals,
# plus, for the initial question, I really don't want to enter an actual
# UTF-8 "NON BREAKABLE SPACE" in my code as it would be undistinguishable
# from a regular space.

gsub("t\xc3\xa9","__",s)  ## works, but I question how standard and portable
# --&gt; "Cet é__."

gsub("t\\xc3\\xa9","__",s)  ## doesn't work
# --&gt; "Cet été."

gsub("t\x{c3a9}","__",s)  ## would work in Perl, doesn't seem to work in R
# Error: '\x' used without hex digits in character string starting "s\x"
```



Here is another answer: (Earlier stuff deleted.)

EDIT2:

```&gt; s <- '\U00A0'
&gt; s
[1] " "
&gt; code <- gsub(s, '__','\xc2\xa0' )
&gt; code
[1] "__"
```

Comment for this answer: I think it should work ... because I tested it. The pattern "\\xc2" matches the `\xc2` character.
Comment for this answer: I admit to a lingering puzzlement about why this _should_ work. I simply took you at your word that `\U00A0` would equal `\xc2\xa0` whereas I would have expected it to equal `\UC2A0`.
Comment for this answer: My expectations were being met by the UTF-16 value.
Comment for this answer: Why do you think that should work?  `"\\xc2"` is a literal slash followed by `"xc2"`, hardly the hex sequence I'm trying to form. In Java, yes, because there is an interpolation of the String done first by the compiler, then, say, `Pattern.compile()` further interprets the String.  But here, no. I also edited my question to try and make it clearer with another example. The `"\\x"` clearly doesn't work.
Comment for this answer: It doesn't, the `\x{c2a0}` is simply passed through, but since it represents Unicode's "NON-BREAK SPACE", you just don't see it in the display. Proof: `gsub("\xc2\xa0","__",gsub("\\xc2\\xa0", " ", "other\xc2\xa0stuff"))` returns: `[1] "other__stuff"`.
Comment for this answer: Thanks! EDIT2 contains exactly what I was looking for ('\Uhhhh' to enter a unicode char). Now, can we please clean up what's before EDIT2?...
Comment for this answer: Indeed, Unicode `U+00A0` is expressed in UTF-8 as `\x{c2a0}` (Perl) or `\xc2\xa0`, as I said. See http://www.fileformat.info/info/unicode/char/a0/index.htm

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:02/06/13', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<r><utf-8>', 'Title': 'Replace a UTF-8 character in R', 'ViewCount': 4876, 'id': 14720493, 'user_AboutMe': '<p>...from banging assembly code in the early eighties to crunching petabytes nowadays, I love Computer Science and things that go fast.</p>\n', 'user_AccountId': 395294.0, 'user_DisplayName': 'Pierre D', 'user_Reputation': 20557.0, 'user_Views': 1303.0, 'user_id': 758174.0}}