File: linebreak.R

package info (click to toggle)
r-cran-kableextra 1.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,400 kB
  • sloc: javascript: 579; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 1,269 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#' Make linebreak in LaTeX Table cells
#'
#' @description This function generates LaTeX code of `makecell` so that users
#' can have linebreaks in their table
#'
#' @param x A character vector
#' @param align Choose from "l", "c" or "r".  Defaults to "l".
#' @param double_escape Whether special character should be double escaped.
#' Default is FALSE.
#' @param linebreaker Symbol for linebreaks to replace. Default is `\n`.
#'
#' @export
linebreak <- function(x, align = c("l", "c", "r"), double_escape = F,
                      linebreaker = "\n") {
  if (is.numeric(x) | is.logical(x)) return(x)
  x <- as.character(x)
  if (missing(align))
    align <- "l"
  align <- vapply(align, match.arg, 'a', choices = c("l", "c", "r"))
  if (double_escape) {
    ifelse(str_detect(x, linebreaker),
           paste0("\\\\makecell[", align, "]{",
                  str_replace_all(x, linebreaker, "\\\\\\\\\\\\\\\\"), "}"),
           x)
  } else {
    ifelse(str_detect(x, linebreaker),
           paste0("\\makecell[", align, "]{",
                  str_replace_all(x, linebreaker, "\\\\\\\\"), "}"),
           x)
  }
}

linebreak_html <- function(x) {
  if (is.numeric(x) | is.logical(x)) return(x)
  ifelse(str_detect(x, "\n"), str_replace_all(x, "\n", "<br />"), x)
}