Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用颜色突出显示R中的文本,以显示预定义的单词列表_R_Text_Highlight_Word - Fatal编程技术网

用颜色突出显示R中的文本,以显示预定义的单词列表

用颜色突出显示R中的文本,以显示预定义的单词列表,r,text,highlight,word,R,Text,Highlight,Word,假设我有一组文档,例如: 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") 我想知道是否有可能使用R为预定义的单词列表突出显示带有颜色的文档(特别是对于大型语料库)。列表中的每个单词都将获得特定的颜色。例如,将“文字”高亮显示为红色,将“文本”高亮显示为蓝色,

假设我有一组文档,例如:

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")
我想知道是否有可能使用R为预定义的单词列表突出显示带有颜色的文档(特别是对于大型语料库)。列表中的每个单词都将获得特定的颜色。例如,将“文字”高亮显示为红色,将“文本”高亮显示为蓝色,如下所示


对于这个问题,这是一个有点粗俗的解决方案,对于大型语料库来说,它的可伸缩性不是很好。我很想知道是否有一种更为节约、优雅、可扩展的方法来实现这一点

库(tidyverse)
图书馆(蜡笔)
#定义文本
文本%
dplyr::select(,-value)
#打印文本
打印(类别(df$value2))


不幸的是,
reprex
不能处理彩色文本,因此无法生成完整的reprex。

Indrajeet的asnwer很棒。这是一个基于Indrajeet答案的答案,只是一点变化

unique_words <- lapply(strsplit(text, " "), function(x){x[!x ==""]})

# creating a dataframe with crayonized text
df <- 
  tibble::enframe(unique_words) %>%
  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) 
唯一单词%
#在这里,您可以指定所需的颜色/单词组合
dplyr::mutate(.data=。,
value2=dplyr::case_当(value==“text”~crayon::blue(value),
value==“words”~蜡笔::红色(value),
真~值))%>%
dplyr::select(,-value)

将输出放在两个不同的行()中:


df这是完整的调试应用程序代码

首先,所需的图书馆:

library(shiny)
library(tidyverse)
library(DT)
library(magrittr)
然后,添加HTML标记的函数:

wordHighlight <- function(SuspWord,colH = 'yellow') {
  paste0('<span style="background-color:',colH,'">',SuspWord,'</span>')
}

如果要将文本保存到HTML文件或其他文件中,这将很容易。您希望将结果保存在何处?颜色应在何处查看?不在R中,但R2wd和officer软件包适合将内容格式化为word文档,然后knittr和markdown对制作html很有用。Marius-我正在使用R shinyapp,结果将在R shinyapp中使用数据表进行演示。谢谢Indrajeet。是的,它没有产生完整的一个,但很好的工作!这对我不起作用。你们是否可以给出一个完整的例子(给出一个虚假的数据)你们的代码是如何在闪亮的应用程序中工作的?一个很好的例子,但并没有正确回答我的问题。例如,如何用红色突出显示单词“数据”,用黄色突出显示单词“术语”。这太棒了。
wordHighlight <- function(SuspWord,colH = 'yellow') {
  paste0('<span style="background-color:',colH,'">',SuspWord,'</span>')
}
ui <- fluidPage(
   titlePanel("Text Highlighting"),
   sidebarLayout(
      sidebarPanel(
        textInput("wordSearch", "Word Search")
      ),
    mainPanel(
        DT::dataTableOutput("table")
      )   
   )  
   )
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)
}
shinyApp(ui = ui, server = server)