根据预定义单词列表突出显示单元格内容中的部分字符串
Highlight part of a string in a cell content based on a list of predefined words
您好,我正在尝试突出显示 DT table 中的单词列表,它根据以下代码为单元格而不是文本着色。有什么方法可以只给文本中的单词上色吗?
例如:如果数据框有 3 列,其中文本列包含(产品的财务税在过去 3 年中增加了 三倍)我只想给三倍着色并加粗。
DT::datatable(data, filter=list(position = 'top', clear = TRUE), rownames = F,class = 'cell-border stripe compact',
options = list(
columnDefs = list(list(className = 'dt-center', targets =c(1,4))),lengthMenu = c(50,100))) %>% formatDate('date', 'toDateString') %>% formatRound('score',3) %>% formatStyle('text', target = 'row',backgroundColor=styleContain("trifold","yellow"))
formatStyle
不会得到您想要的结果,因为它会更改整个元素的 CSS
属性,并且无法对文本的某些部分进行实际样式化。我的建议是实际更改包含 trifold 的每个单元格的内容并将其包装在一些 html 元素中,<mark>
标签似乎是最好的默认情况下适合它会将 background-color
更改为黄色,如果需要,您可以更改以下代码以使用跨度:
# wraps word in the specified tag
wrap_in <- function(.df, column, word, tag){
class<-""
if(grepl("\.", tag)) {
class <- sub(".+?\.(.+)", " class='\1'", tag)
tag <- sub("\..+", "", tag)
}
.df[[column]] <- gsub(sprintf("\b(%s)\b", paste0(word,collapse="|")), sprintf("<%1$s%2$s>\1</%1$s>", tag, class), .df[[column]])
.df
}
# create the data.table
mts %>% wrap_in( "text", "trifold", "mark") %>%
datatable( escape=F, filter=list(position = 'top', clear = TRUE), rownames = F,class = 'cell-border stripe compact',
options = list(columnDefs = list(list(className = 'dt-center', targets =c(1,4))),lengthMenu = c(50,100))) %>% formatRound('carb',3) -> dt
# prepend css to embolden text found in <mark>
dt %>%
htmlwidgets::prependContent(htmltools::tags$style("mark {font-weight: 700;}"))
使用跨度
mts %>% wrap_in( "text", "trifold", "span.highlight") %>%
datatable( escape=F, filter=list(position = 'top', clear = TRUE), rownames = F,class = 'cell-border stripe compact',
options = list(columnDefs = list(list(className = 'dt-center', targets =c(1,4))),lengthMenu = c(50,100))) %>% formatRound('carb',3) %>%
htmlwidgets::prependContent(htmltools::tags$style("span.highlight {color:yellow;font-weight: 700;}"))
数据
mtcars %>% mutate(text=case_when(cyl==6 ~ "Financial duty of the product has increase trifold in last 3 years", T ~ "Financial duty of the product had no significant increase")) -> mts
您好,我正在尝试突出显示 DT table 中的单词列表,它根据以下代码为单元格而不是文本着色。有什么方法可以只给文本中的单词上色吗?
例如:如果数据框有 3 列,其中文本列包含(产品的财务税在过去 3 年中增加了 三倍)我只想给三倍着色并加粗。
DT::datatable(data, filter=list(position = 'top', clear = TRUE), rownames = F,class = 'cell-border stripe compact',
options = list(
columnDefs = list(list(className = 'dt-center', targets =c(1,4))),lengthMenu = c(50,100))) %>% formatDate('date', 'toDateString') %>% formatRound('score',3) %>% formatStyle('text', target = 'row',backgroundColor=styleContain("trifold","yellow"))
formatStyle
不会得到您想要的结果,因为它会更改整个元素的 CSS
属性,并且无法对文本的某些部分进行实际样式化。我的建议是实际更改包含 trifold 的每个单元格的内容并将其包装在一些 html 元素中,<mark>
标签似乎是最好的默认情况下适合它会将 background-color
更改为黄色,如果需要,您可以更改以下代码以使用跨度:
# wraps word in the specified tag
wrap_in <- function(.df, column, word, tag){
class<-""
if(grepl("\.", tag)) {
class <- sub(".+?\.(.+)", " class='\1'", tag)
tag <- sub("\..+", "", tag)
}
.df[[column]] <- gsub(sprintf("\b(%s)\b", paste0(word,collapse="|")), sprintf("<%1$s%2$s>\1</%1$s>", tag, class), .df[[column]])
.df
}
# create the data.table
mts %>% wrap_in( "text", "trifold", "mark") %>%
datatable( escape=F, filter=list(position = 'top', clear = TRUE), rownames = F,class = 'cell-border stripe compact',
options = list(columnDefs = list(list(className = 'dt-center', targets =c(1,4))),lengthMenu = c(50,100))) %>% formatRound('carb',3) -> dt
# prepend css to embolden text found in <mark>
dt %>%
htmlwidgets::prependContent(htmltools::tags$style("mark {font-weight: 700;}"))
使用跨度
mts %>% wrap_in( "text", "trifold", "span.highlight") %>%
datatable( escape=F, filter=list(position = 'top', clear = TRUE), rownames = F,class = 'cell-border stripe compact',
options = list(columnDefs = list(list(className = 'dt-center', targets =c(1,4))),lengthMenu = c(50,100))) %>% formatRound('carb',3) %>%
htmlwidgets::prependContent(htmltools::tags$style("span.highlight {color:yellow;font-weight: 700;}"))
数据
mtcars %>% mutate(text=case_when(cyl==6 ~ "Financial duty of the product has increase trifold in last 3 years", T ~ "Financial duty of the product had no significant increase")) -> mts