-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add kable_* helper functions from lingcod
- Loading branch information
1 parent
f1bd694
commit e9ec331
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#' Create a caption for a longtable that is continued | ||
#' | ||
#' Create a caption that can be used on the repeated pages of a longtable | ||
#' that spans multiple pages. | ||
#' The value returned from this function should be used in | ||
#' [kableExtra::kable_styling] or passed to [kable_styling_sa4ss]. | ||
#' | ||
#' @param caption The caption you want on the subsequent pages. | ||
#' Typically, you will want more than the default argument of nothing, | ||
#' which leads to `... continued.` because this text is pasted onto | ||
#' whatever you pass to this argument. | ||
#' @param max The maximum number of words you want to pull from your | ||
#' caption that you supplied. | ||
#' The default is eight words because this typically leads to just one line. | ||
#' | ||
#' @author Kelli F. Johnson | ||
#' @export | ||
#' | ||
kable_captioncontinue <- function(caption = "", max = 8) { | ||
subcaption <- strsplit(caption, "\\s")[[1]] | ||
out <- paste( | ||
paste( | ||
collapse = " ", | ||
subcaption[1:min(c(max, length(subcaption)))] | ||
), | ||
"... \\emph{continued}." | ||
) | ||
return(out) | ||
} | ||
|
||
#' Formatting for a long table with repeated headers | ||
#' | ||
#' @param ktable A table from [kableExtra::kbl]. | ||
#' @param caption The caption you want to be repeated on the continuing pages | ||
#' of a longtable that spans more than one page. | ||
#' | ||
#' @author Kelli F. Johnson | ||
#' @export | ||
#' | ||
#' @details repeat_header_continued: | ||
#' The argument `repeat_header_continued`, | ||
#' which is passed to [kableExtra::kable_styling], | ||
#' creates a horizontal line at the bottom of your long table on every page. | ||
#' Whereas, when this is `FALSE`, which is the default, | ||
#' then the bottom of a long table is open insinuating that the table continues. | ||
#' You can also add text to this argument that will be displayed below the | ||
#' horizontal line. | ||
#' | ||
#' | ||
kable_styling_sa4ss <- function(ktable, caption) { | ||
ktable %>% kableExtra::kable_styling( | ||
latex_options = c("repeat_header"), | ||
repeat_header_method = "replace", | ||
repeat_header_continued = FALSE, | ||
repeat_header_text = caption | ||
) | ||
} |