Hao Zhu | 501fb45 | 2017-07-05 14:37:13 -0400 | [diff] [blame] | 1 | #' 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" |
| 9 | #' |
| 10 | #' @export |
| 11 | scroll_box <- function(kable_input, height = NULL, width = NULL) { |
| 12 | kable_attrs <- attributes(kable_input) |
| 13 | out <- as.character(kable_input) |
| 14 | box_styles <- c() |
| 15 | if (!is.null(height)) { |
| 16 | box_styles <- c(box_styles, |
| 17 | paste0("overflow-y: scroll; height:", height, "; ")) |
| 18 | } |
| 19 | if (!is.null(width)) { |
| 20 | box_styles <- c(box_styles, |
| 21 | paste0("overflow-x: scroll; width:", width, "; ")) |
| 22 | } |
| 23 | out <- paste0('<div style="', paste(box_styles, collapse = ""), '">', |
| 24 | out, '</div>') |
| 25 | out <- structure(out, format = "html", |
| 26 | class = "knitr_kable") |
| 27 | attributes(out) <- kable_attrs |
| 28 | return(out) |
| 29 | } |