Special characters in textInput


#1

I want a text input that displays the string: ±50ppm/°C

I can do that with a line in UI.R:

textInput("tco","Tempco", value = "\u00B150 ppm/\u00B0C")

However, I have to create the value string programmatically. And the data source I'm using gives me a starting string of:

"<U+00B1>50ppm/<U+00B0>C"

I've been trying all sorts of patterns in gsub to get my second string to turn into the string I need for the textInput but no luck. I can't get anything to produce the single backslash. I'm sure there's some simple way to do this that I'm missing.

Anyone know the answer?
Thanks

(Oddly, standard HTML escape characters work in the label of a textInput, but not the value, where they are rendered literally)


#2

The "\u0000" notation is good for entering the data, but the real goal is to translate the HTML code into Unicode. This is possible if we identify the HTML codes, grab their Unicode numbers, translate those numbers into characters, and then do all the replacements.

library(stringi)
library(magrittr)

parse_html_unicode <- function(x) {
  unicodes <- stri_match_all_regex(x, "<U\\+([0-9a-fA-F]+)>") %>%
    do.call(what = rbind) %>%
    as.data.frame(stringsAsFactors = FALSE) %>%
    setNames(c("html_code", "utf8")) %>%
    unique()

  unicodes[["char"]] <- unicodes[["utf8"]] %>%
    as.hexmode() %>%
    intToUtf8(multiple = TRUE)

  stri_replace_all_fixed(
    x,
    unicodes[["html_code"]],
    unicodes[["char"]],
    vectorize_all = FALSE
  )
}

#3

With a small modification so it doesn't throw an error on strings that don't contain html this is perfect.
Thanks