R/smart_signif.R

Defines functions smart_signif

Documented in smart_signif

#' Minimum preferred significant digits
#'
#' @details
#' Facilitate reducing numbers to their least *distinguishable*
#' significant digits, where "distinguishable" means
#' "between neighbors". This means that if reducing more digits would
#' cause two neighbors to reduce to the same number, then the
#' reduction cannot take place.
#'
#' @seealso [Original question on StackOverflow](https://stackoverflow.com/q/51616332/3358272)
#'
#' @param x numeric, length 2 or more
#' @param digits integer, number of preferred remaining significant digits
#' @param ... unused, kept for compatibility reasons
#' @return numeric vector
#' @section author:
#' [r2evans](https://stackoverflow.com/users/3358272/r2evans)
#' @export
#' @md
#' @examples
#' set.seed(1)
#' x  <- cumsum(abs(rnorm(10,100,100)))
#' smart_signif(x, 1)
#' smart_signif(x, 2)
#' smart_signif(x, 3)
smart_signif <- function(x, digits=3L, ...) {
  stopifnot(length(x) > 1L)
  logscale <- ceiling(log10(abs(x)))
  logdiff <- log10(diff(x))
  keepoom <- floor(pmin(c(Inf, logdiff), c(logdiff, Inf)))
  signif(x, pmax(digits, logscale - keepoom))
}
moodymudskipper/cutr documentation built on Aug. 23, 2019, 7:15 p.m.