1

I'm creating a shiny application and I want to highlight certain predefined words in DT table in shiny. I'm aware of the search highlight feature in DT. For example:

datatable(mtcars2, options = list(searchHighlight = TRUE, search = list(search = 'da')))

I want to highlight like the previous example but not from a search. For say, in mtcars data, I want to highlight words 'Merc', 'Fiat', 'Honda' without providing it in the search of the table. As soon as the table appears, the words will be highlighted, not the entire cell.

Is there a way to do that?

  • 1
    Please read and edit your question according to: [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Add code for `shiny`, `DT` and example of *predefined words* – pogibas Jul 23 '18 at 08:03
  • Is cell that contains word highlight ok? – pogibas Jul 23 '18 at 08:44
  • No, I don't want to color the entire cell. Only the word as I have really long texts. – Riyanka Bhowal Jul 23 '18 at 10:08

1 Answers1

2

You can use tableHTML for that:

library(tableHTML)

The mtcars dataset is use throughout this answer:

Create a tableHTML object using the tableHTML() function. Then apply conditional css, if the column (in this case the rownames, i.e. index 0) contains a specific word. The css that is applied is simply highlighting the background using yellow:

mtcars %>% 
  tableHTML() %>% 
  add_css_conditional_column(columns = 0,
                             conditional = "contains",
                             value = "Toyota",
                             css = list(c("background-color"),
                                        c("yellow")))

The result is:

result_1

In case of many words that should be matched, you can create a vector of words:

words <- c("Merc", "Fiat", "Honda")

Create the basic tableHTML object:

tableHTML <- mtcars %>% 
  tableHTML() 

And apply the css word for word using a loop:

for (word in words) {
  tableHTML <- tableHTML %>% 
    add_css_conditional_column(columns = 0,
                               conditional = "contains",
                               value = word,
                               css = list(c("background-color"),
                                          c("yellow")))
}

The result is:

output_2

If you only want to highlight a certain substring, you could modify the data and include a span around the substring and apply css there.

library(magrittr) # for the %<>% pipe

rownames(mtcars) %<>% 
  stringr::str_replace_all(c('Merc' = '<span style="background-color:yellow">Merc</span>',
                  'Fiat' = '<span style="background-color:yellow">Fiat</span>',
                  'Honda' = '<span style="background-color:yellow">Honda</span>'))


mtcars %>% 
  tableHTML()

The result is:

output_3

clemens
  • 6,653
  • 2
  • 19
  • 31
  • Thank you but I wanted to know if similar solution exists with DT table. I have to incorporate it within a shiny application and tableHTML is not supported by shiny. – Riyanka Bhowal Jul 23 '18 at 09:23
  • You can use tableHTML with shiny, here is the vignette that explains it: https://cran.r-project.org/web/packages/tableHTML/vignettes/tableHTML.html – clemens Jul 23 '18 at 09:26
  • 1
    Well, thanks for sharing the documentation. But it colors the entire cell, I want to color the word only otherwise it doesn't make sense in my context. – Riyanka Bhowal Jul 23 '18 at 10:06
  • it is possible if you are willing to insert spans. i have updated the answer with a possible workaround. – clemens Jul 23 '18 at 10:55
  • Hi, it's been a while but still I couldn't figure out how it would work on a specific column. I was trying out on **iris** dataset. This is the piece of code: `iris$Species %<>% stringr::str_replace_all(c('setosa' = 'setosa', 'virginica' = 'virginica')) iris %>% tableHTML()` but this replaces the string with the span command – Riyanka Bhowal Nov 13 '18 at 16:51
  • use `escape = FALSE` in `tableHTML()` – clemens Nov 14 '18 at 01:52