Search code examples
rdt

How to set bold row in datatable?


I am using datatable() function to visualize dataframe in HTML output, now I want to set the 4th and 8th rows in bold

df <- data.frame (origin = c("A","B","C","D","E","F","G","H","I","J"),
              Percentage = c(23,16,32,71,3,60,15,21,44,60),
              rate = c(10,12,20,200,-25,12,13,90,-105,23),
              change = c(10,12,-5,12,6,8,0.5,-2,5,-2))

library(DT)

# no clear buttons
datatable(df, options = list(autoWidth = TRUE, initComplete = JS("
                        function(settings, json) {
                          $(this.api().table().header()).css({
                          'font-size': '12px',
                          });
                        }
                    ")), filter = list(
  position = 'top', clear = FALSE
))%>%
  formatStyle(columns = colnames(.$x$data), `font-size` = "11px")

enter image description here


Solution

  • We could use:

    library(DT)
    datatable(df, options = list(autoWidth = TRUE, initComplete = JS("
                            function(settings, json) {
                              $(this.api().table().header()).css({
                              'font-size': '12px',
                              });
                            }
                        ")), filter = list(
                          position = 'top', clear = FALSE ))%>%
     
      formatStyle(
        0,
        target = "row",
        fontWeight = styleEqual(c(4, 8), "bold"),
        `font-size`="11px"
      )
    

    output:

    enter image description here