diff --git a/NAMESPACE b/NAMESPACE index 3955a5b4..f53efeec 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -49,6 +49,7 @@ S3method(end_window,yearweek) S3method(fill_gaps,data.frame) S3method(fill_gaps,tbl_ts) S3method(format,interval) +S3method(format,summarycal) S3method(format,tbl_ts) S3method(format,yearmonth) S3method(format,yearquarter) @@ -133,6 +134,8 @@ S3method(start_window,yearquarter) S3method(start_window,yearweek) S3method(summarise,grouped_ts) S3method(summarise,tbl_ts) +S3method(summary,yearmonth) +S3method(summary,yearquarter) S3method(tbl_sum,grouped_ts) S3method(tbl_sum,tbl_ts) S3method(time_ts,Date) diff --git a/R/summary.R b/R/summary.R new file mode 100644 index 00000000..ccb715ca --- /dev/null +++ b/R/summary.R @@ -0,0 +1,38 @@ +#' @export +summary.yearquarter <- function(object, digits = 12L, ...) { + x <- summary.default(unclass(object), digits = digits, ...) + if (m <- match("NA's", names(x), 0L)) { + NAs <- as.integer(x[m]) + x <- x[-m] + attr(x, "NAs") <- NAs + } + output <- yearquarter(.Date(unclass(x))) + attributes(output) <- attributes(x) + class(output) <- c("summarycal", class(object)) + output +} + +#' @export +summary.yearmonth <- function(object, digits = 12L, ...) { + x <- summary.default(unclass(object), digits = digits, ...) + if (m <- match("NA's", names(x), 0L)) { + NAs <- as.integer(x[m]) + x <- x[-m] + attr(x, "NAs") <- NAs + } + output <- yearmonth(.Date(unclass(x))) + attributes(output) <- attributes(x) + class(output) <- c("summarycal", class(object)) + output +} + +#' @export +format.summarycal <- function(x, ...) { + xx <- x + class(xx) <- class(x)[-1] + xx <- c( + format(xx), + `NA's` = if (length(a <- attr(x, "NAs"))) as.character(a) + ) + xx +} diff --git a/R/yearquarter.R b/R/yearquarter.R index 698a4573..3c5cc306 100644 --- a/R/yearquarter.R +++ b/R/yearquarter.R @@ -125,7 +125,8 @@ yearquarter.yearquarter <- function(x, fiscal_start = attr(x, "fiscal_start")) { mth <- fiscal_start - fs new_yearquarter( new_date(x) + period(year = -(fs == 1) + (fiscal_start == 1), month = mth), - fiscal_start) + fiscal_start + ) } #' @export @@ -316,13 +317,17 @@ format.yearquarter <- function(x, format = "%Y Q%q", ...) { qtr <- round(yrqtr %% 1 * 10) qtr_sub <- map_chr(formatC(qtr), function(z) gsub("%q", z, x = format)) qtr_sub[is.na(qtr_sub)] <- "-" # NA formats cause errors - format.Date(make_date(yr, qtr * 3 - 2), format = qtr_sub) + dates <- make_date(yr, qtr * 3 - 2) + names(dates) <- names(x) + format.Date(dates, format = qtr_sub) } #' @rdname tsibble-vctrs #' @export obj_print_data.yearquarter <- function(x, ...) { - if (length(x) == 0) return() + if (length(x) == 0) { + return() + } print(format(x)) } @@ -394,4 +399,3 @@ intersect.yearquarter <- set_ops("yearquarter", op = "intersect") #' @export setdiff.yearquarter <- set_ops("yearquarter", op = "setdiff") -