Nothing
# Newer tsibble versions define their own methods for mixtime indices. These
# registrations would otherwise replace them (depending on load order), so
# defer to tsibble's method whenever it provides one.
tsibble_mixtime_method <- function(generic) {
get0(
paste0(generic, ".mixtime::mixtime"),
envir = asNamespace("tsibble"),
mode = "function",
inherits = FALSE
)
}
index_valid <- S7::new_external_generic("tsibble", "index_valid", "x")
method(index_valid, class_mixtime) <- function(x) {
if (!is.null(f <- tsibble_mixtime_method("index_valid"))) return(f(x))
TRUE
}
interval_pull <- S7::new_external_generic("tsibble", "interval_pull", "x")
method(interval_pull, class_mixtime) <- function(x) {
if (!is.null(f <- tsibble_mixtime_method("interval_pull"))) return(f(x))
intvls <- lapply(x@x, function(x) tsibble::interval_pull(x))
x <- new_rcrd(
vec_rbind(!!!lapply(intvls, new_data_frame)),
.regular = any(vapply(intvls, attr, logical(1L), which = ".regular")),
class = c("mixtime_interval", "interval")
)
}
method(interval_pull, mt_time) <- function(x) {
chronon <- attr(x, "chronon")
interval <- list(chronon@n)
names(interval) <- unit_name(chronon, 1L)
rlang::inject(tsibble::new_interval(!!!interval))
}
#' @exportS3Method base::format mixtime_interval
format.mixtime_interval <- function(x, ...) {
fmt <- vapply(
vctrs::new_rcrd(unclass(x)),
fmt_interval,
# function(y) format(`class<-`(y, "interval")),
character(1L)
)
# tsibble requires length 1 interval formatting
paste(unique(fmt), collapse = ", ")
}
fmt_interval <- function (x, ...) {
if (!attr(x, ".regular"))
return("!")
if (all(vec_proxy(x) == 0))
return("?")
n <- n_fields(x)
micro <- ifelse(cli::is_utf8_output(), "\u00b5s", "us")
defaults <- vec_c("Y", "Q", "M", "W", "D", "h", "m", "s",
"ms", micro, "ns", "")
n_defaults <- vec_size(defaults)
misc <- if (n > n_defaults)
vec_slice(fields(x), (n_defaults + 1):n)
else NULL
fmt_names <- vec_c(defaults, misc)
val <- unlist(unclass(x), use.names = FALSE)
paste0(val[val != 0], fmt_names[val != 0], collapse = " ")
}
#' @exportS3Method base::print mixtime_interval
print.mixtime_interval <- function(x, ...) cat(format(x, ...))
#' @export
vec_proxy.mixtime_interval <- function(x, ...) {
# Bypass tsibble:::unknown_interval length 1 checks
apply(new_data_frame(x), 2, sum, simplify = FALSE)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.