2

I am trying to make a long list of unicode characters "\u1", "\u2", "\u3", ... "\u10000"

I tried

paste0("\u",1:10000)

However the backslash is treated as an escape character and I get an error.

How can I insert the backslash into my character strings without it being treated as an escape character?

(I realise this question has been tackled before, Escaping a backslash with a backslash in R produces 2 backslashes in a string, not 1 but the solution of using cat("\\") does not work for my situation)

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
Tim K
  • 71
  • 7
  • 2
    `paste0("\\u",1:10000)` is giving me list like `\u1,\u2....` is this what you want ? – Code Maniac Sep 02 '19 at 01:08
  • 1
    Escaping a backslash gives a double backslash. So ```paste0("\\u",1:10000)``` gives me ```"\\u1", "\\u2", "\\u3", ... "\\u10000"```, which does not evaluate as unicode characters as I want – Tim K Sep 02 '19 at 02:47

2 Answers2

2

This is actually quite an interesting question. Note that simply pasteing \\u won't work by itself, because that will get you the two characters "\" and "u". Adding a number to the end won't magically change it into a Unicode character.

I think the most straightforward way is to build an expression as a string, and then eval it:

nums <- 1:10
x <- paste0("\\u", nums)
x <- paste0('"', x, '"', collapse=",")
eval(parse(text=paste("c(", x, ")")))
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
1

How about using cat() after the paste0() funciton. This should give you the answer:

cat(paste0('\\u', 1:1000))
Mahdiar
  • 104
  • 10