'Using ascii in str_replace

I am working on a R package that has a function for replacing characters like ü,ä, etc. If I check the package, I get the warning:

Portable packages must use only ASCII characters in their R code, except perhaps in comments. Use \uxxxx escapes for other characters.

The function contains

  n <- str_replace_all(n, c('ü' = 'ue', 'ï' = 'ie', "ä" = 'ae','ö' = 'oe'))

I tried replacing the ü with "\u00fc" and the same thing for the others. But this doesn't work.

str_replace("uüe", \\u00cf, "ue")
str_replace("uüe", *\\u00cf", "ue")
str_replace("uüe", <+u00cf>, "ue")

Any idea, how to do this?



Solution 1:[1]

As far as I can see you're using the package stringr. That does allow using and manipulating non-ASCII chars.

For example:

vec <- "ärmlich nicht über den wolken, höchstens hïmmlisch"

Set the names of the strings you want to replace with setNames:

ref <- setNames(c("ue", "ie", "ae", "oe"),
                c("ü", "ï", "ä", "ö"))

Feed this set into the str_replace_alloperation:

library(stringr)
str_replace_all(vec, ref)
[1] "aermlich nicht ueber den wolken, hoechstens hiemmlisch"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris Ruehlemann