Combining elements of two datatables into one new table


I have this relatively simple problem I hope I can get a solution for. I want one datatable that's the combination of the two tables below. The values that should show on the table should come from df_value while the conditional formatting for each cell should be based on df_count. If the cell value in df_count is less than 10 then the corresponding cell should be highlted red on the final datatable output while showing the value from the second table.


df_count <- data.frame(
           A = c(27L, 2L, 46L, 30L, 34L, 1L, 48L, 1L, 8L, 17L, 17L, 8L, 25L, 7L),
           B = c(47L,16L,44L,9L,42L,39L,36L,8L,32L,
           C = c(18L,32L,41L,2L,42L,29L,4L,12L,50L,
           D = c(42L,31L,44L,41L,2L,28L,6L,31L,22L,
           E = c(47L,17L,15L,5L,34L,12L,27L,24L,4L,


df_value <- data.frame(
             A = c(95L,69L,4L,68L,3L,90L,47L,50L,
             B = c(47L,17L,54L,3L,79L,9L,34L,20L,
             C = c(69L,96L,79L,56L,73L,5L,45L,25L,
             D = c(37L,41L,49L,61L,41L,7L,52L,99L,
             E = c(42L,18L,41L,15L,53L,49L,3L,34L,

Created on 2020-10-12 by the reprex package (v0.3.0)


new_df <- map2_dfc(df_count,df_value,
             paste0('<div style="background-color:#D11;">',.y,'</div>'), # try span as alternative to DIV

DT::datatable(data = new_df,
              escape = FALSE)
1 Like

Thank you for respnding @nirgrahamuk. This actually seems to be it really :slight_smile: Will span do the whole cell instead?

no, div does the whole cell, span does the text only

1 Like

I am perfectly happy with the whole cell. Thank you for this. I have a feeling this will solve my problem. Going to implement it shortly on my existing problem :slight_smile:

you can try this for a different style.

DT::datatable(data = new_df,
              escape = FALSE,

@nirgrahamuk, thanks so much for the help so far.

I just wanted to ask. How can I change the new datatable output back into the original formatting?

This is what I am getting from the datatable

This is what I originally had. I just want to add the total from the bottom and the search bar back unto the top one

please share a reprex for the code you used to make your total fields , search bar and your other formatting


See here


table_values <- pivotr(diamonds, 
                     cvars = c("cut","clarity"),
                     nvar = "price",
                     fun = "sum") %>% dtab()

There are two possibilities

  1. make your own version of pivotr that builds tables how you want them built.
  2. don't use pivotr but use the method we have for making DT and just add more to it, like calculate your own totals and add them in.
    you can use the same HTML styling approach to bold them etc.
    there will be an option to turn on search you could look up in the DT documentation

Thanks again for your original solution @nirgrahamuk. I have found a mixed solution for the moment which is sufficient. Going to have to play more with DT to see exactly how to get the tables like I want them from scratch.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.