R/format_cells.R

Defines functions format_cells

Documented in format_cells

#' Format cells of a data frame
#' From https://stackoverflow.com/questions/28166168/how-to-change-fontface-bold-italics-for-a-cell-in-a-kable-table-in-rmarkdown
#'@param df data frame
#'@param rows  row(s) of the cell(s) to format
#'@param cols  column(s) of the cell(s) to format
#'@param value format to apply: one of "italics", "bold", "strikethrough"
#'@export
#'
format_cells <- function(df, rows ,cols, value = c("italics", "bold", "strikethrough")){

  # select the correct markup
  # one * for italics, two ** for bold
  map <- setNames(c("*", "**", "~~"), c("italics", "bold", "strikethrough"))
  markup <- map[value]

  for (r in rows){
    for(c in cols){

      # Make sure values are not factors
      df[[c]] <- as.character( df[[c]])

      # Update formatting
      df[r, c] <- paste0(markup, df[r, c], markup)
    }
  }

  return(df)
}
dco61/isteahMAT104 documentation built on Jan. 2, 2022, 2:24 a.m.