blob: 44bd096a93ec3b1596cb691312d7c775ba630f1d [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 Zhu501fb452017-07-05 14:37:13 -040011#'
12#' @export
Hao Zhu4b0c51e2017-08-01 15:21:07 -040013scroll_box <- function(kable_input, height = NULL, width = NULL,
14 box_css = "border: 1px solid #ddd; padding: 5px; ",
15 extra_css = NULL) {
Hao Zhu501fb452017-07-05 14:37:13 -040016 kable_attrs <- attributes(kable_input)
17 out <- as.character(kable_input)
Hao Zhu4b0c51e2017-08-01 15:21:07 -040018 box_styles <- c(box_css, extra_css)
Hao Zhu501fb452017-07-05 14:37:13 -040019 if (!is.null(height)) {
20 box_styles <- c(box_styles,
21 paste0("overflow-y: scroll; height:", height, "; "))
22 }
23 if (!is.null(width)) {
24 box_styles <- c(box_styles,
25 paste0("overflow-x: scroll; width:", width, "; "))
26 }
27 out <- paste0('<div style="', paste(box_styles, collapse = ""), '">',
28 out, '</div>')
29 out <- structure(out, format = "html",
30 class = "knitr_kable")
31 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -050032 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu501fb452017-07-05 14:37:13 -040033 return(out)
34}