blob: e090a5e795a07570f7483faaf4a70daf6efd60f1 [file] [log] [blame]
Hao Zhu248bbef2018-04-02 18:25:14 -04001#' Make linebreak in LaTeX Table cells
2#'
Hao Zhu5e4dd502018-04-05 12:01:58 -04003#' @description This function generate LaTeX code of `makecell` so that users
4#' can have linebreaks in their table
5#'
6#' @param x A character vector
7#' @param align Choose from "l", "c" or "r"
8#' @param double_escape Whether special character should be double escaped.
9#' Default is FALSE.
Hao Zhub548aac2018-09-18 13:29:01 -040010#' @param linebreaker Symbol for linebreaks to replace. Default is `\\n`.
Hao Zhu5e4dd502018-04-05 12:01:58 -040011#'
Hao Zhu248bbef2018-04-02 18:25:14 -040012#' @export
Hao Zhua17edd02018-09-15 21:20:44 -040013linebreak <- function(x, align = c("l", "c", "r"), double_escape = F,
14 linebreaker = "\n") {
Hao Zhu248bbef2018-04-02 18:25:14 -040015 if (is.numeric(x) | is.logical(x)) return(x)
16 align <- match.arg(align, c("l", "c", "r"))
17 if (double_escape) {
Hao Zhua17edd02018-09-15 21:20:44 -040018 ifelse(str_detect(x, linebreaker),
Hao Zhu248bbef2018-04-02 18:25:14 -040019 paste0("\\\\makecell[", align, "]{",
Hao Zhua17edd02018-09-15 21:20:44 -040020 str_replace_all(x, linebreaker, "\\\\\\\\\\\\\\\\"), "}"),
Hao Zhu248bbef2018-04-02 18:25:14 -040021 x)
22 } else {
Hao Zhua17edd02018-09-15 21:20:44 -040023 ifelse(str_detect(x, linebreaker),
Hao Zhu248bbef2018-04-02 18:25:14 -040024 paste0("\\makecell[", align, "]{",
Hao Zhua17edd02018-09-15 21:20:44 -040025 str_replace_all(x, linebreaker, "\\\\\\\\"), "}"),
Hao Zhu248bbef2018-04-02 18:25:14 -040026 x)
27 }
28}
Hao Zhu5e4dd502018-04-05 12:01:58 -040029
30linebreak_html <- function(x) {
31 if (is.numeric(x) | is.logical(x)) return(x)
32 ifelse(str_detect(x, "\n"), str_replace_all(x, "\n", "<br />"), x)
33}