Skip to content

Commit db645ef

Browse files
committed
fixed non-stnadard issue. but needs testing. renv out of sync, build_site fails
1 parent c2b13a6 commit db645ef

File tree

7 files changed

+188
-72
lines changed

7 files changed

+188
-72
lines changed

R/methods.R

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ create_calendar <- S7::new_generic("create_calendar","x")
88
calculate <- S7::new_generic("calculate","x")
99

1010

11-
complete_calendar <- S7::new_generic("complete_calendar","x")
1211

1312

1413
#' Create Calendar Table
@@ -39,7 +38,7 @@ S7::method(create_calendar,ti) <- function(x){
3938

4039
start_date <- closest_sunday_feb1(min_year)
4140

42-
if(min_year<start_date){
41+
if(min_year<lubridate::year(start_date)){
4342

4443
start_date <- closest_sunday_feb1(min_year-1)
4544

@@ -99,6 +98,9 @@ S7::method(create_calendar,ti) <- function(x){
9998
,!!x@value@value_vec:= dplyr::coalesce(!!x@value@value_quo, 0)
10099
)
101100

101+
102+
103+
102104
return(full_dbi)
103105
}
104106

@@ -167,55 +169,6 @@ S7::method(calculate,segment_abc) <- function(x){
167169
}
168170

169171

170-
#' @title complete_calendar
171-
#' @name complete_calendar
172-
#' @param x ti object
173-
#' @returns dbi object
174-
#' @keywords internal
175-
S7::method(complete_calendar,ti) <- function(x){
176-
177-
178-
calendar_dbi<- x@datum@data |>
179-
dplyr::count(!!x@datum@date_quo) |>
180-
dplyr::select(-n)
181-
182-
183-
date_vec <- x@datum@date_vec
184-
185-
out <- calendar_dbi |>
186-
dplyr::mutate(
187-
year_start_date=lubridate::floor_date(!!x@datum@date_quo,unit = "year")
188-
,year_end_date=dplyr::sql(glue::glue("date_trunc('year', {date_vec}) + INTERVAL '1' YEAR"))
189-
,quarter_start_date=lubridate::floor_date(!!x@datum@date_quo,unit = "quarter")
190-
,quarter_end_date=dplyr::sql(glue::glue("date_trunc('quarter', {date_vec}) + INTERVAL '1' quarter"))
191-
,month_start_date=lubridate::floor_date(!!x@datum@date_quo,unit = "month")
192-
,month_end_date=dplyr::sql(glue::glue("date_trunc('month', {date_vec}) + INTERVAL '1' month"))
193-
,week_start_date=lubridate::floor_date(!!x@datum@date_quo,unit = "week")
194-
,week_end_date=dplyr::sql(glue::glue("date_trunc('month', {date_vec}) + INTERVAL '1' month"))
195-
,day_of_week=lubridate::wday(!!x@datum@date_quo,label = FALSE)
196-
,day_of_week_label=lubridate::wday(!!x@datum@date_quo,label = TRUE)
197-
,days_in_year=year_end_date-year_start_date
198-
,days_in_quarter=quarter_end_date-quarter_start_date
199-
,days_in_month=dplyr::sql(glue::glue("last_day({date_vec})"))
200-
,days_complete_in_week=!!x@datum@date_quo-week_start_date
201-
,days_remaining_in_week=week_end_date-!!x@datum@date_quo
202-
,days_remaining_in_quarter=quarter_end_date-!!x@datum@date_quo
203-
,days_remaining_in_month=month_end_date-!!x@datum@date_quo
204-
,days_remaining_in_year=year_end_date-!!x@datum@date_quo
205-
,days_complete_in_year=!!x@datum@date_quo-year_start_date
206-
,days_complete_in_quarter=!!x@datum@date_quo-quarter_start_date
207-
,days_complete_in_month=!!x@datum@date_quo-month_start_date
208-
,days_complete_in_year=!!x@datum@date_quo-year_start_date
209-
,weekend_indicator=dplyr::if_else(day_of_week_label %in% c("Saturday","Sunday"),1,0)
210-
) |>
211-
dplyr::mutate(
212-
dplyr::across(dplyr::contains("date"),\(x) as.Date(x))
213-
)
214-
215-
return(out)
216-
217-
}
218-
219172

220173

221174
#' @title Print ti objects
@@ -418,3 +371,55 @@ S7::method(print,segment_cohort) <- function(x,...){
418371
print_next_steps()
419372

420373
}
374+
375+
376+
377+
# complete_calendar <- S7::new_generic("complete_calendar","x")
378+
379+
380+
381+
# S7::method(complete_calendar,ti) <- function(x){
382+
#
383+
#
384+
# calendar_dbi<- x@datum@data |>
385+
# dplyr::count(!!x@datum@date_quo) |>
386+
# dplyr::select(-n)
387+
#
388+
#
389+
# date_vec <- x@datum@date_vec
390+
#
391+
# out <- calendar_dbi |>
392+
# dplyr::mutate(
393+
# year_start_date=lubridate::floor_date(!!x@datum@date_quo,unit = "year")
394+
# ,year_end_date=dplyr::sql(glue::glue("date_trunc('year', {date_vec}) + INTERVAL '1' YEAR"))
395+
# ,quarter_start_date=lubridate::floor_date(!!x@datum@date_quo,unit = "quarter")
396+
# ,quarter_end_date=dplyr::sql(glue::glue("date_trunc('quarter', {date_vec}) + INTERVAL '1' quarter"))
397+
# ,month_start_date=lubridate::floor_date(!!x@datum@date_quo,unit = "month")
398+
# ,month_end_date=dplyr::sql(glue::glue("date_trunc('month', {date_vec}) + INTERVAL '1' month"))
399+
# ,week_start_date=lubridate::floor_date(!!x@datum@date_quo,unit = "week")
400+
# ,week_end_date=dplyr::sql(glue::glue("date_trunc('month', {date_vec}) + INTERVAL '1' month"))
401+
# ,day_of_week=lubridate::wday(!!x@datum@date_quo,label = FALSE)
402+
# ,day_of_week_label=lubridate::wday(!!x@datum@date_quo,label = TRUE)
403+
# ,days_in_year=year_end_date-year_start_date
404+
# ,days_in_quarter=quarter_end_date-quarter_start_date
405+
# ,days_in_month=dplyr::sql(glue::glue("last_day({date_vec})"))
406+
# ,days_complete_in_week=!!x@datum@date_quo-week_start_date
407+
# ,days_remaining_in_week=week_end_date-!!x@datum@date_quo
408+
# ,days_remaining_in_quarter=quarter_end_date-!!x@datum@date_quo
409+
# ,days_remaining_in_month=month_end_date-!!x@datum@date_quo
410+
# ,days_remaining_in_year=year_end_date-!!x@datum@date_quo
411+
# ,days_complete_in_year=!!x@datum@date_quo-year_start_date
412+
# ,days_complete_in_quarter=!!x@datum@date_quo-quarter_start_date
413+
# ,days_complete_in_month=!!x@datum@date_quo-month_start_date
414+
# ,days_complete_in_year=!!x@datum@date_quo-year_start_date
415+
# ,weekend_indicator=dplyr::if_else(day_of_week_label %in% c("Saturday","Sunday"),1,0)
416+
# ) |>
417+
# dplyr::mutate(
418+
# dplyr::across(dplyr::contains("date"),\(x) as.Date(x))
419+
# )
420+
#
421+
# return(out)
422+
#
423+
# }
424+
425+

R/utils-misc.R

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,11 @@ create_non_standard_month <- function(.data,pattern){
581581
#' @param .data data
582582
#' @param pattern 554,445 or 545
583583
#' @param x is ti object
584-
#'
584+
#' @keywords internal
585585
#' @returns DBI object
586586
#'
587-
augment_non_standard_calendar <- function(.data,x,pattern){
587+
complete_non_standard_calendar <- function(.data,x){
588+
588589

589590
#test inputs
590591
# pattern <- "544"
@@ -593,6 +594,11 @@ augment_non_standard_calendar <- function(.data,x,pattern){
593594
days_in_week=7
594595
weeks_in_quarter=13
595596
quarters_in_year=4
597+
old_cols <- colnames(.data)
598+
date_cols <- x@fn@new_date_column_name[x@fn@new_date_column_name!="date"]
599+
new_cols <- lubridate::union(old_cols,new_cols)
600+
pattern <- x@datum@calendar_type
601+
596602
# start_year <- closest_sunday_feb1(min_year)
597603
min_date <- x@datum@min_date
598604

@@ -632,11 +638,93 @@ augment_non_standard_calendar <- function(.data,x,pattern){
632638
date>min_date
633639
)
634640

641+
642+
out <- out |>
643+
dplyr::select(dplyr::any_of(new_cols))
644+
635645
return(out)
636646

637647
}
638648

639649

650+
#' Title
651+
#'
652+
#' @param .data DBI object
653+
#' @param x ti object
654+
#' @keywords internal
655+
#' @returns DBI object
656+
#'
657+
complete_standard_calendar <- function(.data,x){
658+
659+
660+
if(any(x@fn@new_date_column_name %in% "year")){
661+
662+
.data <- .data |>
663+
dplyr::mutate(
664+
year=lubridate::year(date)
665+
)
666+
667+
}
668+
if(any(x@fn@new_date_column_name %in% "quarter")){
669+
670+
.data <- .data |>
671+
dplyr::mutate(
672+
year=lubridate::quarter(date)
673+
)
674+
675+
}
676+
677+
if(any(x@fn@new_date_column_name %in% "month")){
678+
679+
.data <- .data |>
680+
dplyr::mutate(
681+
year=lubridate::month(date)
682+
)
683+
684+
}
685+
686+
if(any(x@fn@new_date_column_name %in% "day")){
687+
688+
.data <- .data |>
689+
dplyr::mutate(
690+
year=lubridate::day(date)
691+
)
692+
693+
}
694+
695+
return(.data)
696+
697+
}
698+
699+
700+
#' Title
701+
#'
702+
#' @param x ti object
703+
#' @keywords internal
704+
#'
705+
#' @returns DBI object
706+
#'
707+
create_full_dbi <- function(x){
708+
709+
710+
if(x@datum@calendar_type=="standard"){
711+
712+
full_dbi <- create_calendar(x) |>
713+
complete_standard_calendar(x=x)
714+
715+
}
716+
717+
if(x@datum@calendar_type!="standard"){
718+
719+
full_dbi <- create_calendar(x) |>
720+
complete_non_standard_calendar(x=x)
721+
}
722+
723+
return(full_dbi)
724+
725+
}
726+
727+
640728
utils::globalVariables(
641729
c(
642730
"desc",

_pkgdown.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
desc: "General helper and execution utilities."
1717
contents:
1818
- augment_standard_calendar
19-
- augment_non_standard_calendar
2019
- calculate
2120

2221
- title: "Cohort & Classification"

man/complete_calendar.Rd

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/complete_standard_calendar.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/create_full_dbi.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)