R/dt2_html.R

Defines functions dt2_col_template dt2_cols_html

Documented in dt2_cols_html dt2_col_template

#' Allow raw HTML rendering via `columns.render`
#'
#' @description Mark columns to render raw HTML using a JS render function.
#' @param options Options list.
#' @param cols Names or 1-based indices.
#' @param js_render JS function (via [htmlwidgets::JS]) with signature `(data, type, row, meta)`
#'   returning a string of HTML when `type == "display"`.
#' @return Updated `options`.
#' @export
dt2_cols_html <- function(options = list(), cols, js_render) {
  cols <- .dt2_name_to_idx(cols, options)
  cds <- lapply(cols, function(i) list(
    targets = i - 1L,
    render  = js_render
  ))
  options$columnDefs <- c(options$columnDefs %||% list(), cds)
  options
}

#' Simple HTML template per column (replace `{{VAL}}`)
#'
#' @param options Options list.
#' @param col Name or index of target column.
#' @param template HTML string with `{{VAL}}` placeholder.
#' @return Updated `options`.
#' @export
dt2_col_template <- function(options = list(), col, template) {
  col <- .dt2_name_to_idx(col, options)
  js <- htmlwidgets::JS(
    sprintf(
      "function(d,t,row,meta){ if(t!=='display') return d; var html=%s; return html.replace(/\\{\\{VAL\\}\\}/g, d); }",
      jsonlite::toJSON(template, auto_unbox = TRUE)
    )
  )
  dt2_cols_html(options, col, js)
}

Try the DT2 package in your browser

Any scripts or data that you put into this service are public.

DT2 documentation built on June 14, 2026, 9:06 a.m.