R/text_styling.R

Defines functions merge_with_txt_gp fp_align_right fp_align_center fp_align_left fp_txt_gp fp_txt_plain fp_txt_bold fp_txt_italic

Documented in fp_align_center fp_align_left fp_align_right fp_txt_bold fp_txt_gp fp_txt_italic fp_txt_plain

#' Text styling
#'
#' This is a collection of functions to allow styling of text
#'
#' @param txt The text to styl
#' @returns A list of txt with style attributes
#'
#' @examples
#' fp_txt_italic("Italic text")
#' @export
#' @rdname text_styling
fp_txt_italic <- function(txt)  {
  sapply(txt, \(str) {
    txt_gp <- attr(str, "txt_gp")
    if (is.null(txt_gp)) {
      txt_gp <- gpar()
    }
    txt_gp$fontface <- "italic"
    attr(str, "txt_gp")  <- txt_gp
    return(str)
  },
  simplify = FALSE,
  USE.NAMES = FALSE)
}

#' @export
#' @rdname text_styling
fp_txt_bold <- function(txt)  {
  sapply(txt, \(str) {
    txt_gp <- attr(str, "txt_gp")
    if (is.null(txt_gp)) {
      txt_gp <- gpar()
    }
    txt_gp$fontface <- "bold"
    attr(str, "txt_gp")  <- txt_gp
    return(str)
  },
  simplify = FALSE,
  USE.NAMES = FALSE)
}

#' @export
#' @rdname text_styling
fp_txt_plain <- function(txt)  {
  sapply(txt, \(str) {
    txt_gp <- attr(str, "txt_gp")
    if (is.null(txt_gp)) {
      txt_gp <- gpar()
    }
    txt_gp$fontface <- "plain"
    attr(str, "txt_gp")  <- txt_gp
    return(str)
  },
  simplify = FALSE,
  USE.NAMES = FALSE)
}

#' @export
#' @rdname text_styling
#' @param gp A [grid::gpar()] style to apply
fp_txt_gp <- function(txt, gp)  {
  sapply(txt, \(str) {
    txt_gp <- attr(str, "txt_gp")
    if (is.null(txt_gp)) {
      txt_gp <- gpar()
    }
    for (n in names(gp)) {
      txt_gp[[n]] <- gp[[n]]
    }

    attr(str, "txt_gp")  <- txt_gp
    return(str)
  },
  simplify = FALSE,
  USE.NAMES = FALSE)
}

#' @export
#' @rdname text_styling
fp_align_left <- function(txt)  {
  sapply(txt, \(str) {
    attr(str, "align")  <- "l"
    return(str)
  },
  simplify = FALSE,
  USE.NAMES = FALSE)
}

#' @export
#' @rdname text_styling
fp_align_center <- function(txt)  {
  sapply(txt, \(str) {
    attr(str, "align")  <- "c"
    return(str)
  },
  simplify = FALSE,
  USE.NAMES = FALSE)
}

#' @export
#' @rdname text_styling
fp_align_right <- function(txt)  {
  sapply(txt, \(str) {
    attr(str, "align")  <- "r"
    return(str)
  },
  simplify = FALSE,
  USE.NAMES = FALSE)
}



merge_with_txt_gp <- function(gp_list, txt_out) {
  txt_gp <- attr(txt_out, "txt_gp")
  if (is.null(txt_gp)) {
    return(gp_list)
  }

  for (n in names(txt_gp)) {
    gp_list[[n]] <- txt_gp[[n]]
  }

  return(gp_list)
}

Try the forestplot package in your browser

Any scripts or data that you put into this service are public.

forestplot documentation built on Aug. 26, 2023, 5:07 p.m.