在R中将字符转换为html

Phi*_*ipp 18 html r

R中将包含非ASCII字符的字符(向量)转换为html的首选方法是什么?我想比如转换

  "ü"
Run Code Online (Sandbox Code Playgroud)

  "ü"
Run Code Online (Sandbox Code Playgroud)

我知道这可以通过聪明地使用gsub(但是有人一劳永逸地做过吗?)而且我认为R2HTML包可以做到这一点,但事实并非如此.

编辑:这是我最终使用的; 通过修改字典显然可以扩展它:

char2html <- function(x){
  dictionary <- data.frame(
    symbol = c("ä","ö","ü","Ä", "Ö", "Ü", "ß"),
    html = c("&auml;","&ouml;", "&uuml;","&Auml;",
             "&Ouml;", "&Uuml;","&szlig;"))
  for(i in 1:dim(dictionary)[1]){
    x <- gsub(dictionary$symbol[i],dictionary$html[i],x)
  }
  x
}

x <- c("Buschwindröschen", "Weißdorn")
char2html(x)
Run Code Online (Sandbox Code Playgroud)

Dom*_*ois 3

这个问题很老了,但我找不到任何简单的答案...所以我想出了这个简单的函数,它使用数字 html 代码并适用于LATIN 1 - Supplement(整数值 161 到 255)。某些包中可能(当然?)有一个函数可以更彻底地完成此操作,但接下来的内容对于许多应用程序来说可能已经足够好了......

\n\n
conv_latinsupp <- function(...) {\n  out <- character()\n  for (s in list(...)) {\n    splitted <- unlist(strsplit(s, ""))\n    intvalues <- utf8ToInt(enc2utf8(s))\n    pos_to_modify <- which(intvalues >=161 & intvalues <= 255)\n    splitted[pos_to_modify] <- paste0("&#0",  intvalues[pos_to_modify], ";")\n    out <- c(out, paste0(splitted, collapse = ""))\n  }\n  out\n}\n\nconv_latinsupp("aeiou", "\xc3\xa0\xc3\xa9\xc3\xaf\xc3\xb4\xc3\xb912345")\n## [1] "aeiou"   "&#0224;&#0233;&#0239;&#0244;&#0249;12345"\n
Run Code Online (Sandbox Code Playgroud)\n