html - 在R中将字符转换为html

标签 html r

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

  "ü"

  "ü"

我知道这可以通过巧妙地使用 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)

最佳答案

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

conv_latinsupp <- function(...) {
  out <- character()
  for (s in list(...)) {
    splitted <- unlist(strsplit(s, ""))
    intvalues <- utf8ToInt(enc2utf8(s))
    pos_to_modify <- which(intvalues >=161 & intvalues <= 255)
    splitted[pos_to_modify] <- paste0("&#0",  intvalues[pos_to_modify], ";")
    out <- c(out, paste0(splitted, collapse = ""))
  }
  out
}

conv_latinsupp("aeiou", "àéïôù12345")
## [1] "aeiou"   "&#0224;&#0233;&#0239;&#0244;&#0249;12345"

关于html - 在R中将字符转换为html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018570/

相关文章:

c# - Css display none 不适用于 div 及其内容

r - 嵌套列表中的唯一类

r - 应用总和/平均值/等。到一个列表

r - 使用 data.table 的 R 中每个因子的频率

html - 边距/间距从何而来?

javascript - 改变输入值

r - 粘贴中“collapse”的缩写?

R拆分成组以并行执行

javascript - 如果类存在则将类添加到嵌套 div,如果类不存在则将类添加到所有嵌套 div?

html - 如何使用 HTML/CSS 和 Bootstrap 使视频适合网页的背景?