File: as.datatable.R

package info (click to toggle)
r-cran-formattable 0.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 420 kB
  • sloc: javascript: 15; sh: 12; makefile: 2
file content (30 lines) | stat: -rw-r--r-- 1,063 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#' Generic function to create an htmlwidget
#'
#' This function is a generic function to create an \code{htmlwidget}
#' to allow HTML/JS from R in multiple contexts.
#'
#' @param x an object.
#' @param ... arguments to be passed to \code{\link[DT]{datatable}}
#' @export
#' @return a \code{\link[DT]{datatable}} object
as.datatable <- function(x, ...) {
  UseMethod("as.datatable")
}

#' Convert \code{formattable} to a \code{\link[DT]{datatable}} htmlwidget
#'
#' @param x a \code{formattable} object to convert
#' @param escape \code{logical} to escape \code{HTML}. The default is
#'          \code{FALSE} since it is expected that \code{formatters} from
#'          \code{formattable} will produce \code{HTML} tags.
#' @param ... additional arguments passed to to \code{\link[DT]{datatable}}
#' @return a \code{\link[DT]{datatable}} object
#' @export
as.datatable.formattable <- function(x, escape = FALSE, ...) {
  stopifnot(is.formattable(x), requireNamespace("DT"))
  DT::datatable(
    render_html_matrix.formattable(x),
    escape = escape,
    ...
  )
}