blob: 39405cf25805f3aacc2865e7f93a5d933b1e7b65 [file] [log] [blame]
Hao Zhu501fb452017-07-05 14:37:13 -04001#' Put a HTML table into a scrollable box
2#'
3#' @description This function will put a HTML kable object in a fixed-height,
4#' fixed-width or both box and make it scrollable.
5#'
6#' @param kable_input A HTML kable object
7#' @param height A character string indicating the height of the box, e.g. "50px"
8#' @param width A character string indicating the width of the box, e.g. "100px"
Hao Zhu4b0c51e2017-08-01 15:21:07 -04009#' @param box_css CSS text for the box
10#' @param extra_css Extra CSS styles
Hao Zhu72917f92019-03-15 18:41:42 -040011#' @param fixed_thead HTML table option so table header row is fixed at top.
12#' Values can be either T/F or `list(enabled = T/F, background = "anycolor")`.
Hao Zhu501fb452017-07-05 14:37:13 -040013#'
14#' @export
Irene319a5462018-04-17 15:37:39 -070015#'
16#' @examples
17#' \dontrun{
18#' # Specify table size by pixels
19#' kable(cbind(mtcars, mtcars), "html") %>%
20#' kable_styling() %>%
21#' scroll_box(width = "500px", height = "200px")
22#'
23#' # Specify by percent
24#' kable(cbind(mtcars, mtcars), "html") %>%
25#' kable_styling() %>%
26#' scroll_box(width = "100%", height = "200px")
27#' }
Hao Zhu4b0c51e2017-08-01 15:21:07 -040028scroll_box <- function(kable_input, height = NULL, width = NULL,
29 box_css = "border: 1px solid #ddd; padding: 5px; ",
Hao Zhu72917f92019-03-15 18:41:42 -040030 extra_css = NULL,
31 fixed_thead = TRUE
32 ) {
Jrmieaa304982018-12-08 00:14:16 +010033
Hao Zhu501fb452017-07-05 14:37:13 -040034 kable_attrs <- attributes(kable_input)
Hao Zhu72917f92019-03-15 18:41:42 -040035 fixed_thead <- get_fixed_thead(fixed_thead)
36 if (is.null(height)) fixed_thead$enabled <- FALSE
Jrmieaa304982018-12-08 00:14:16 +010037
38 if (fixed_thead$enabled) {
39 box_css = "border: 1px solid #ddd; padding: 0px; "
40 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu72917f92019-03-15 18:41:42 -040041 all_header_cells <- xml2::xml_find_all(kable_xml, "//thead//th")
42 if (is.null(fixed_thead$background)) fixed_thead$background <- "#FFFFFF"
43 for (i in seq(length(all_header_cells))) {
44 xml_attr(all_header_cells[i], "style") <- paste0(
45 xml_attr(all_header_cells[i], "style"),
46 "position: sticky; top:0; background-color: ",
47 fixed_thead$background, ";"
48 )
Jrmieaa304982018-12-08 00:14:16 +010049 }
50 out <- as.character(as_kable_xml(kable_xml))
51 } else {
52 out <- as.character(kable_input)
53 }
54
Hao Zhu4b0c51e2017-08-01 15:21:07 -040055 box_styles <- c(box_css, extra_css)
Jrmieaa304982018-12-08 00:14:16 +010056
Hao Zhu501fb452017-07-05 14:37:13 -040057 if (!is.null(height)) {
58 box_styles <- c(box_styles,
59 paste0("overflow-y: scroll; height:", height, "; "))
60 }
Jrmieaa304982018-12-08 00:14:16 +010061
Hao Zhu501fb452017-07-05 14:37:13 -040062 if (!is.null(width)) {
63 box_styles <- c(box_styles,
64 paste0("overflow-x: scroll; width:", width, "; "))
65 }
Jrmieaa304982018-12-08 00:14:16 +010066
Hao Zhu501fb452017-07-05 14:37:13 -040067 out <- paste0('<div style="', paste(box_styles, collapse = ""), '">',
68 out, '</div>')
69 out <- structure(out, format = "html",
70 class = "knitr_kable")
71 attributes(out) <- kable_attrs
Jrmieaa304982018-12-08 00:14:16 +010072
Hao Zhuf2100832018-01-11 16:20:29 -050073 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Jrmieaa304982018-12-08 00:14:16 +010074
Hao Zhu501fb452017-07-05 14:37:13 -040075 return(out)
76}
Hao Zhu72917f92019-03-15 18:41:42 -040077
78get_fixed_thead <- function(x) {
79 if (is.logical(x)) {
80 if (x) return(list(enabled = TRUE, background = "#FFFFFF"))
81 return(list(enabled = FALSE))
82 }
83 return(x)
84}