blob: b0660ed229399fd79140936cf7dce942349d6e2f [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
Jrmieaa304982018-12-08 00:14:16 +010011#' @param fixed_thead A list of two named element. enabled and background.
12#' Default is F and white, e.g. "list(enabled = T, background = "#fff")"
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#' }
28
Hao Zhu4b0c51e2017-08-01 15:21:07 -040029scroll_box <- function(kable_input, height = NULL, width = NULL,
30 box_css = "border: 1px solid #ddd; padding: 5px; ",
Jrmieaa304982018-12-08 00:14:16 +010031 extra_css = NULL, fixed_thead = list(enabled = F, background = "#fff")) {
32
Hao Zhu501fb452017-07-05 14:37:13 -040033 kable_attrs <- attributes(kable_input)
Jrmieaa304982018-12-08 00:14:16 +010034
35 if (fixed_thead$enabled) {
36 box_css = "border: 1px solid #ddd; padding: 0px; "
37 kable_xml <- read_kable_as_xml(kable_input)
38 kable_thead <- xml_tpart(kable_xml, "thead")
39 original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
40 for (theader_i in 1:length(xml_children(original_header_row))) {
41 target_header_cell <- xml_child(original_header_row, theader_i)
42 xml_attr(target_header_cell, "style") <- paste0(xml_attr(target_header_cell, "style"),
43 "position: sticky; top:0; background: ",
44 fixed_thead$background,";")
45 }
46 out <- as.character(as_kable_xml(kable_xml))
47 } else {
48 out <- as.character(kable_input)
49 }
50
Hao Zhu4b0c51e2017-08-01 15:21:07 -040051 box_styles <- c(box_css, extra_css)
Jrmieaa304982018-12-08 00:14:16 +010052
Hao Zhu501fb452017-07-05 14:37:13 -040053 if (!is.null(height)) {
54 box_styles <- c(box_styles,
55 paste0("overflow-y: scroll; height:", height, "; "))
56 }
Jrmieaa304982018-12-08 00:14:16 +010057
Hao Zhu501fb452017-07-05 14:37:13 -040058 if (!is.null(width)) {
59 box_styles <- c(box_styles,
60 paste0("overflow-x: scroll; width:", width, "; "))
61 }
Jrmieaa304982018-12-08 00:14:16 +010062
Hao Zhu501fb452017-07-05 14:37:13 -040063 out <- paste0('<div style="', paste(box_styles, collapse = ""), '">',
64 out, '</div>')
65 out <- structure(out, format = "html",
66 class = "knitr_kable")
67 attributes(out) <- kable_attrs
Jrmieaa304982018-12-08 00:14:16 +010068
Hao Zhuf2100832018-01-11 16:20:29 -050069 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Jrmieaa304982018-12-08 00:14:16 +010070
Hao Zhu501fb452017-07-05 14:37:13 -040071 return(out)
72}