Skip to content

Commit

Permalink
index_by
Browse files Browse the repository at this point in the history
  • Loading branch information
huizezhang-sherry committed Aug 4, 2024
1 parent a9b2894 commit c84c4a7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ S3method(fill_gaps,temporal_cubble_df)
S3method(filter,spatial_cubble_df)
S3method(group_by,spatial_cubble_df)
S3method(group_by,temporal_cubble_df)
S3method(index_by,temporal_cubble_df)
S3method(key,cubble_df)
S3method(key_data,cubble_df)
S3method(key_vars,cubble_df)
Expand Down Expand Up @@ -123,6 +124,7 @@ importFrom(tidyr,unnest)
importFrom(tsibble,as_tsibble)
importFrom(tsibble,fill_gaps)
importFrom(tsibble,index)
importFrom(tsibble,index_by)
importFrom(tsibble,key)
importFrom(tsibble,key_data)
importFrom(tsibble,key_vars)
Expand Down
1 change: 1 addition & 0 deletions R/cubble-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' @importFrom rlang .data quo_is_missing sym as_name
#' @importFrom cli cli_abort cli_inform
#' @importFrom tsibble index as_tsibble
#' @importFrom tsibble fill_gaps scan_gaps index_by
#' @aliases cubble-package
#' @keywords internal
"_PACKAGE"
Expand Down
15 changes: 14 additions & 1 deletion R/dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ summarise.spatial_cubble_df <- function(.data, ..., .by = NULL,
#' @export
summarise.temporal_cubble_df <- function(.data, ..., .by = key_vars(.data),
.groups = NULL){
#browser()
vars <- enquos(..., .named = TRUE)
index <- index_var(.data)
key <- key_vars(.data)
Expand All @@ -247,10 +248,22 @@ summarise.temporal_cubble_df <- function(.data, ..., .by = key_vars(.data),
index <- setdiff(potential_index, key)
}

if (!index %in% colnames(out) || !key %in% colnames(out)){
if (is.null(attr(.data, "index2"))) {
idx_in <- index %in% colnames(out)
} else{
idx_in <- index %in% colnames(out) && attr(.data, "index2") %in% colnames(out)
}

if (!idx_in || !key %in% colnames(out)){
return(as_tibble(out))
}

index <- if (index %in% colnames(out)){
index <- index
} else{
index <- attr(.data, "index2")
}

new_temporal_cubble(
out, key = key, index = index, coords = coords, spatial = spatial)
}
Expand Down
20 changes: 19 additions & 1 deletion R/tsibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ make_temporal_tsibble <- function(x){

#' Gap-filling on the temporal component of a cubble object
#' @inheritParams tsibble::fill_gaps
#' @importFrom tsibble fill_gaps scan_gaps
#' @return a cubble object
#' @rdname tsibble
#' @export
Expand Down Expand Up @@ -62,3 +61,22 @@ scan_gaps.temporal_cubble_df <- function(.data, ...){


}

#' @rdname tsibble
#' @export
index_by.temporal_cubble_df <- function(.data, ...){
#browser()
stopifnot(is_cubble_temporal(.data))
key <- key_vars(.data)
index <- index_var(.data)
coords <- coords(.data)
spatial <- spatial(.data)
.data <- as_tsibble(.data, key = key , index = index)
res <- index_by(.data, ...)

new_temporal_cubble(
res, key = key, index = index, coords = coords(.data), spatial = spatial)


}

10 changes: 1 addition & 9 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@
s3_register("dplyr::rowwise", "spatial_cubble_df")
s3_register("dplyr::rowwise", "temporal_cubble_df")

#s3_register("tsibble::index_by", "cubble_df")
#s3_register("dplyr::ungroup", "cubble_df")

# s3_register("dplyr::slice_head", "cubble_df")
# s3_register("dplyr::slice_tail", "cubble_df")
# s3_register("dplyr::slice_min", "cubble_df")
# s3_register("dplyr::slice_max", "cubble_df")
# s3_register("dplyr::slice_sample", "cubble_df")

s3_register("base::print", "cubble_df")
s3_register("tsibble::fill_gaps", "temporal_cubble_df")
s3_register("tsibble::scan_gaps", "temporal_cubble_df")
s3_register("tsibble::index_by", "temporal_cubble_df")
s3_register("tsibble::key", "cubble_df")
s3_register("tsibble::key_data", "cubble_df")
s3_register("tsibble::key_vars", "cubble_df")
Expand Down
2 changes: 1 addition & 1 deletion man/accessors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/tsibble.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c84c4a7

Please sign in to comment.