R/utilities.r

Defines functions ggpretty match.fun.null alpha tryapply tryNULL defaults compact stamp

Documented in alpha

# Pretty axis breaks
ggpretty <- function(x) {
  unique(c(min(x), grid.pretty(x), max(x)))
}

# Version of \code{\link{match.fun}} that returns NULL on failure
match.fun.null <- function(x) {
  f <- NULL
  try(f <- match.fun(x), silent=TRUE)
  f
}

#' Give a colour an alpha level
#'
#' @param colour colour
#' @param alpha alpha level [0,1]
#' @keywords internal
#' @export
alpha <- function(colour, alpha) {
	col <- grDevices::col2rgb(colour, TRUE) / 255
	col[4, ] <- alpha
	grDevices::rgb(col[1,], col[2,], col[3,], col[4,])
}

# Apply with built in try
tryapply <- function(list, fun, ...) {
  compact(lapply(list, function(x) tryNULL(fun(x, ...))))
}

tryNULL <- function(expr)  {
  result <- NULL
  tryCatch(result <- expr, error=function(e){})
  result
}

defaults <- function(x, y) {
  c(x, y[setdiff(names(y), names(x))])
}

compact <- function(x) {
  x[!vapply(x, is.null, logical(1))]
}

stamp <- function(...) {
  suppressWarnings(reshape::stamp(...))
}

#' Pipe operator
#'
#' See \code{\link[magrittr]{\%>\%}} for more details.
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
NULL
hadley/ggplot1 documentation built on Aug. 19, 2019, 2:42 p.m.