R/full_seq.R

Defines functions round_any fullseq.POSIXt fullseq.numeric fullseq

Documented in fullseq

#' Generate sequence of fixed size intervals covering range.
#'
#' @param range range
#' @param size interval size
#' @param ... other arguments passed on to methods
#' @keywords internal
#' @export
fullseq <- function(range, size, ...) UseMethod("fullseq")

#' @export
fullseq.numeric <- function(range, size, ..., pad = FALSE) {
  # if (zero_range(range)) return(range + size * c(-1, 1) / 2)

  x <- seq(
    round_any(range[1], size, floor),
    round_any(range[2], size, ceiling),
    by = size
  )

  if (pad) {
    # Add extra bin on bottom and on top, to guarantee that we cover complete
    # range of data, whether right = T or F
    c(min(x) - size, x, max(x) + size)
  } else {
    x
  }
}

#' @export
fullseq.POSIXt <- function(range, size, ...) {
  seq(range[1], range[2], by = size)
}
#' @export
fullseq.Date <- fullseq.POSIXt

round_any <- function(x, accuracy, f = round) {
  f(x / accuracy) * accuracy
}

Try the ggvis package in your browser

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

ggvis documentation built on May 29, 2024, 1:12 a.m.