server <- function(input, output) {
sentence <- "The term 'data science' (originally used interchangeably with 'datalogy') has existed for over thirty years and was used initially as a substitute for computer science by Peter Naur in 1960."
sentence2 = "One of the things we will want to do most often for social science analyses of text data is generate a document-term matrix."
YourData = data.frame(N = c('001','002'), T = c(sentence,sentence2), stringsAsFactors=FALSE)
highlightData <- reactive({
if (input$wordSearch!="")
{
patterns = input$wordSearch
YourData2 = YourData
YourData2[,2] %<>% str_replace_all(regex(patterns, ignore_case = TRUE), wordHighlight)
return(YourData2)
}
return(YourData)
})
output$table <- DT::renderDataTable({
data <- highlightData()
}, escape = FALSE)
}
library(tidyverse)
library(crayon)
# define text
text <- c("is it possible to highlight text for some words" ,
"suppose i want words like words to be red and words like text to be blue")
# individuate words
unique_words <- function(x) {
purrr::map(.x = x,
.f = ~ unique(base::strsplit(x = ., split = " ")[[1]],
collapse = " "))
}
# creating a dataframe with crayonized text
df <-
tibble::enframe(unique_words(x = text)) %>%
tidyr::unnest() %>%
# here you can specify the color/word combinations you need
dplyr::mutate(.data = .,
value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
value == "words" ~ crayon::red(value),
TRUE ~ value)) %>%
dplyr::select(., -value)
# printing the text
print(cat(df$value2))
4条答案
按热度按时间fkvaft9z1#
这里是完整的调试应用程序代码!
第一,所需的库:
然后,添加HTML标记的函数:
当前UI部分:
最后,在服务器端:
运行应用程序:
wnavrhmk2#
对于这个问题,这是一个有点笨拙的解决方案,对于大型语料库来说不是很容易扩展。我很好奇是否有一个更简洁、优雅和可扩展的方法来实现这一点。
不幸的是,
reprex
不能处理彩色文本,所以不能生成完整的reprex。lhcgjxsq3#
Indrajeet的回答很棒。这是一个基于Indrajeet的答案的答案,只是一点点变化。
在两条不同的线(Collapse text by group in data frame)中具有输出:
如果我想在R控制台中打印,答案看起来可以。如果我想在R shinyapp中输出,它是如何工作的?
寻找其他替代方案,并感谢您的帮助。
jvlzgdj94#
这是突出显示文档中存在的单词列表的代码。这段代码在Rmarkdown中用于创建word文档
{r突出显示=真}
单词列表〈- c(“苹果”、“香蕉”、“梨”)