R/labs_glue.R

Defines functions labs_glue

Documented in labs_glue

#' Generate ggplot2 labels based on data in a ggtibble
#'
#' @param p The ggtibble object
#' @param ... Named arguments to be used as `ggplot2::labs()` labels where the
#'   value is a glue specification
#' @returns `p` with the labels modified
#' @export
labs_glue <- function(p, ...) {
  stopifnot(inherits(p, "ggtibble"))
  args <- list(...)
  stopifnot(length(args) > 0)
  # all ... arguments must be named
  stopifnot(!is.null(names(args)))
  stopifnot(!any(names(args) %in% ""))
  labs_args <- list()
  for (nm in names(args)) {
    labs_args[[nm]] <- glue::glue_data(p, args[[nm]])
    if (length(labs_args[[nm]]) == 0) {
      # handle NULL inputs
      labs_args[[nm]] <- rep(list(labs_args[[nm]]), nrow(p))
    }
  }
  purrr::pmap(.l = labs_args, .f = ggplot2::labs)
}

Try the ggtibble package in your browser

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

ggtibble documentation built on June 22, 2024, 10:55 a.m.