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" |
Hao Zhu | 4b0c51e | 2017-08-01 15:21:07 -0400 | [diff] [blame] | 9 | #' @param box_css CSS text for the box |
| 10 | #' @param extra_css Extra CSS styles |
Hao Zhu | 501fb45 | 2017-07-05 14:37:13 -0400 | [diff] [blame] | 11 | #' |
| 12 | #' @export |
Hao Zhu | 4b0c51e | 2017-08-01 15:21:07 -0400 | [diff] [blame] | 13 | scroll_box <- function(kable_input, height = NULL, width = NULL, |
| 14 | box_css = "border: 1px solid #ddd; padding: 5px; ", |
| 15 | extra_css = NULL) { |
Hao Zhu | 501fb45 | 2017-07-05 14:37:13 -0400 | [diff] [blame] | 16 | kable_attrs <- attributes(kable_input) |
| 17 | out <- as.character(kable_input) |
Hao Zhu | 4b0c51e | 2017-08-01 15:21:07 -0400 | [diff] [blame] | 18 | box_styles <- c(box_css, extra_css) |
Hao Zhu | 501fb45 | 2017-07-05 14:37:13 -0400 | [diff] [blame] | 19 | 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 |
| 32 | return(out) |
| 33 | } |