在 HTML 中为 R Shiny 着色和突出显示文本
Colorizing and highlighting text in HTML for R Shiny
我 运行 在大型语料库上使用了一堆 NLP 算法,我想探索我的结果。换句话说,我希望能够根据我的模型提取的特征在 HTML 中格式化文本,以便在 Shiny web 应用程序中查看它们,如下所示:
由于我对html一无所知,您能告诉我应该考虑哪些方式来指导我的研究吗?是否存在一些 R 包来执行此类任务?闪亮的功能是否足够?如果有,有哪些功能?
不确定这是否准确回答了问题,但可能相关:
require(tidyverse)
require(spacyr)
require(shiny)
tempDir <- tempfile()
dir.create(tempDir)
htmlFile <- file.path(tempDir, "index.html")
viewer <- getOption("viewer")
s = spacy_parse(sample(stringr::sentences, 25), dependency=T, nounphrase=T) %>% as_tibble()
print(s)
# A tibble: 224 x 11
doc_id sentence_id token_id token lemma pos head_token_id dep_rel entity nounphrase
<chr> <int> <int> <chr> <chr> <chr> <dbl> <chr> <chr> <chr>
1 text1 1 1 Brea… brea… VERB 1 ROOT "" ""
2 text1 1 2 deep deep ADJ 1 acomp "" ""
3 text1 1 3 and and CCONJ 1 cc "" ""
4 text1 1 4 smell smell VERB 1 conj "" ""
5 text1 1 5 the the DET 7 det "" "beg"
6 text1 1 6 piny piny ADJ 7 amod "" "mid"
7 text1 1 7 air air NOUN 4 dobj "" "end_root"
8 text1 1 8 . . PUNCT 1 punct "" ""
9 text10 1 1 Fill fill VERB 1 ROOT "" ""
10 text10 1 2 the the DET 4 det "" "beg"
# … with 214 more rows, and 1 more variable: whitespace <lgl>
cols = sample(rainbow(15))
pos_types = s %>% count(pos, sort=T) %>% print() %>% .[['pos']]
sink(htmlFile)
cat('<h4>')
for(i in 1:nrow(s)){
col = cols[match(s$pos[i], pos_types)]
cat(as.character(span(s$token[i], style=glue::glue('color:{col}'),
title = paste(s$pos[i], s$dep_rel[i]))))
cat(' ')
}
cat('</h4>')
sink()
viewer(htmlFile)
我 运行 在大型语料库上使用了一堆 NLP 算法,我想探索我的结果。换句话说,我希望能够根据我的模型提取的特征在 HTML 中格式化文本,以便在 Shiny web 应用程序中查看它们,如下所示:
由于我对html一无所知,您能告诉我应该考虑哪些方式来指导我的研究吗?是否存在一些 R 包来执行此类任务?闪亮的功能是否足够?如果有,有哪些功能?
不确定这是否准确回答了问题,但可能相关:
require(tidyverse)
require(spacyr)
require(shiny)
tempDir <- tempfile()
dir.create(tempDir)
htmlFile <- file.path(tempDir, "index.html")
viewer <- getOption("viewer")
s = spacy_parse(sample(stringr::sentences, 25), dependency=T, nounphrase=T) %>% as_tibble()
print(s)
# A tibble: 224 x 11
doc_id sentence_id token_id token lemma pos head_token_id dep_rel entity nounphrase
<chr> <int> <int> <chr> <chr> <chr> <dbl> <chr> <chr> <chr>
1 text1 1 1 Brea… brea… VERB 1 ROOT "" ""
2 text1 1 2 deep deep ADJ 1 acomp "" ""
3 text1 1 3 and and CCONJ 1 cc "" ""
4 text1 1 4 smell smell VERB 1 conj "" ""
5 text1 1 5 the the DET 7 det "" "beg"
6 text1 1 6 piny piny ADJ 7 amod "" "mid"
7 text1 1 7 air air NOUN 4 dobj "" "end_root"
8 text1 1 8 . . PUNCT 1 punct "" ""
9 text10 1 1 Fill fill VERB 1 ROOT "" ""
10 text10 1 2 the the DET 4 det "" "beg"
# … with 214 more rows, and 1 more variable: whitespace <lgl>
cols = sample(rainbow(15))
pos_types = s %>% count(pos, sort=T) %>% print() %>% .[['pos']]
sink(htmlFile)
cat('<h4>')
for(i in 1:nrow(s)){
col = cols[match(s$pos[i], pos_types)]
cat(as.character(span(s$token[i], style=glue::glue('color:{col}'),
title = paste(s$pos[i], s$dep_rel[i]))))
cat(' ')
}
cat('</h4>')
sink()
viewer(htmlFile)