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 |
Jrmie | aa30498 | 2018-12-08 00:14:16 +0100 | [diff] [blame^] | 11 | #' @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 Zhu | 501fb45 | 2017-07-05 14:37:13 -0400 | [diff] [blame] | 13 | #' |
| 14 | #' @export |
Irene | 319a546 | 2018-04-17 15:37:39 -0700 | [diff] [blame] | 15 | #' |
| 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 Zhu | 4b0c51e | 2017-08-01 15:21:07 -0400 | [diff] [blame] | 29 | scroll_box <- function(kable_input, height = NULL, width = NULL, |
| 30 | box_css = "border: 1px solid #ddd; padding: 5px; ", |
Jrmie | aa30498 | 2018-12-08 00:14:16 +0100 | [diff] [blame^] | 31 | extra_css = NULL, fixed_thead = list(enabled = F, background = "#fff")) { |
| 32 | |
Hao Zhu | 501fb45 | 2017-07-05 14:37:13 -0400 | [diff] [blame] | 33 | kable_attrs <- attributes(kable_input) |
Jrmie | aa30498 | 2018-12-08 00:14:16 +0100 | [diff] [blame^] | 34 | |
| 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 Zhu | 4b0c51e | 2017-08-01 15:21:07 -0400 | [diff] [blame] | 51 | box_styles <- c(box_css, extra_css) |
Jrmie | aa30498 | 2018-12-08 00:14:16 +0100 | [diff] [blame^] | 52 | |
Hao Zhu | 501fb45 | 2017-07-05 14:37:13 -0400 | [diff] [blame] | 53 | if (!is.null(height)) { |
| 54 | box_styles <- c(box_styles, |
| 55 | paste0("overflow-y: scroll; height:", height, "; ")) |
| 56 | } |
Jrmie | aa30498 | 2018-12-08 00:14:16 +0100 | [diff] [blame^] | 57 | |
Hao Zhu | 501fb45 | 2017-07-05 14:37:13 -0400 | [diff] [blame] | 58 | if (!is.null(width)) { |
| 59 | box_styles <- c(box_styles, |
| 60 | paste0("overflow-x: scroll; width:", width, "; ")) |
| 61 | } |
Jrmie | aa30498 | 2018-12-08 00:14:16 +0100 | [diff] [blame^] | 62 | |
Hao Zhu | 501fb45 | 2017-07-05 14:37:13 -0400 | [diff] [blame] | 63 | 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 |
Jrmie | aa30498 | 2018-12-08 00:14:16 +0100 | [diff] [blame^] | 68 | |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 69 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Jrmie | aa30498 | 2018-12-08 00:14:16 +0100 | [diff] [blame^] | 70 | |
Hao Zhu | 501fb45 | 2017-07-05 14:37:13 -0400 | [diff] [blame] | 71 | return(out) |
| 72 | } |