highlight specific words in a column with kable

Is it possible to highlight specific words in a column within the kable function?

data frame:

tb <- tibble(text=c("this image is awesome", "this image is awful"), likes=c(34,8))
tb
# A tibble: 2 x 2
  text                  likes
  <chr>                 <dbl>
1 this image is awesome    34
2 this image is awful       8

within a shinyapp i placed a interactive function in which a user can search for a specific word in a text corpus. if a user is looking for "awesome", i want only the word awesome in a html table to be highlighted.

I assume you have checked out the packages kableExtra and flextable?

If they can't do the job, is it possible in your application to edit the raw HTML code?

library(tidyverse)
tb <- tibble(text=c("this image is awesome", "this image is awful"), likes=c(34,8))
tb

library(knitr)
t1 <- kable(tb, format="html")

search_word <- "awesome"
replace_string <- paste0("<b>", search_word, "</b>")
t2 <- gsub(search_word, replace_string, t1)

writeLines(t1, con="test_t1.html")
writeLines(t2, con="test_t2.html")

HTH

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.