Commit 39708068 authored by houyun's avatar houyun
Browse files

helper function about ggplot axis labels

parent b1dee674
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -111,6 +111,7 @@ export(hyplot)
export(is_grouped_matrix_data)
export(is_matrix_data)
export(is_md_tbl)
export(label_formula)
export(latex_expression)
export(latex_richtext)
export(layout_tbl_graph_circular)

R/gg-utils.R

0 → 100644
+41 −0
Original line number Diff line number Diff line
#' Label formula
#' @description \code{label_formula()} can be used to parse text label to expression
#' or richtext, and displays labels as math formula.
#' @param parse logical or function generated by \code{parse_func()}.
#' @return a label function.
#' @rdname label_formula
#' @author Hou Yun
#' @export
label_formula <- function (parse = TRUE) {
  force(parse)
  function(x) {
    if (length(x) < 1) {
      return(character(0))
    }
    if (!is.function(parse) && !isTRUE(parse)) {
      return(x)
    }

    n <- length(x)
    nm <- names(x)
    .FUN <- parse
    print(parse)
    if (!is.function(.FUN)) {
      if (any(grepl("\n", x, fixed = TRUE))) {
        .FUN <- parse_func(output = "richtext")
      } else {
        .FUN <- parse_func(output = "character")
      }
    }
    x <- .FUN(x)

    if (!is_richtext(x)) {
      x <- parse_safe(x)
    }
    if (is_richtext(x)) {
      message("It should be used in conjunction with 'ggtext::element_markdown()'.")
    }
    names(x) <- nm
    x
  }
}

man/label_formula.Rd

0 → 100644
+21 −0
Original line number Diff line number Diff line
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/gg-utils.R
\name{label_formula}
\alias{label_formula}
\title{Label formula}
\usage{
label_formula(parse = TRUE)
}
\arguments{
\item{parse}{logical or function generated by \code{parse_func()}.}
}
\value{
a label function.
}
\description{
\code{label_formula()} can be used to parse text label to expression
or richtext, and displays labels as math formula.
}
\author{
Hou Yun
}