@@ -77,55 +77,6 @@ S7::method(create_calendar,ti) <- function(x){
7777}
7878
7979
80-
81- # ' Create Calendar Table
82- # ' @name create_calendar
83- # ' @param x segment object
84- # '
85- # ' @returns dbi object
86- # ' @export
87- # ' @description
88- # ' `create_calendar()` summarizes a tibble to target time unit and completes the calendar to ensure
89- # ' no missing days, month, quarter or years. If a grouped tibble is passed through it will complete the calendar
90- # ' for each combination of the group
91- # ' @details
92- # ' This is in internal function to make it easier to ensure data has no missing dates to
93- # ' simplify the use of time intelligence functions downstream of the application.
94- # ' If you want to summarize to a particular group, simply pass the tibble through to the [dplyr::group_by()] argument
95- # ' prior to function and the function will make summarize and make a complete calendar for each group item.
96- # ' @keywords internal
97- S7 :: method(create_calendar ,segment ) <- function (x ){
98-
99- # # summarize data table
100- summary_dbi <- x @ datum @ data | >
101- dplyr :: mutate(
102- date = lubridate :: floor_date(!! x @ datum @ date_quo ,unit = " day" )
103- ) | >
104- dplyr :: summarise(
105- !! x @ value @ value_vec : = sum(!! x @ value @ value_quo ,na.rm = TRUE )
106- ,.by = c(date ,!!! x @ datum @ group_quo )
107- )
108-
109- # create calendar table
110-
111- calendar_dbi <- seq_date_sql(start_date = x @ datum @ min_date ,end_date = x @ datum @ max_date ,time_unit = x @ time_unit @ value ,con = dbplyr :: remote_con(x @ datum @ data )) | >
112- dplyr :: cross_join(
113- summary_dbi | >
114- dplyr :: distinct(!!! x @ value @ value_quo )
115- )
116-
117-
118-
119- # Perform a full join to ensure all time frames are represented
120- full_dbi <- dplyr :: full_join(
121- calendar_dbi
122- ,summary_dbi
123- ,by = dplyr :: join_by(date ,!!! x @ value @ value_quo )
124- )
125-
126- return (full_dbi )
127- }
128-
12980# ' @title Execute time-intelligence or segments class objects to return the underlying transformed table
13081# ' @name calculate
13182# ' @param x ti object
@@ -161,7 +112,7 @@ S7::method(calculate,ti) <- function(x){
161112# ' abc(category_values = c(.3,.5,.75,.85)) |>
162113# ' calculate()
163114# '}
164- S7 :: method(calculate ,segment ) <- function (x ){
115+ S7 :: method(calculate ,segment_cohort ) <- function (x ){
165116
166117 out <- x @ fn @ fn_exec(x )
167118
@@ -171,6 +122,29 @@ S7::method(calculate,segment) <- function(x){
171122
172123
173124
125+ # ' @title Execute time-intelligence or segments class objects to return the underlying transformed table
126+ # ' @name calculate
127+ # ' @param x segment object
128+ # '
129+ # ' @returns dbi object
130+ # ' @export
131+ # ' @examples
132+ # ' \dontrun{
133+ # ' sales |>
134+ # ' group_by(store_key) |>
135+ # ' abc(category_values = c(.3,.5,.75,.85)) |>
136+ # ' calculate()
137+ # '}
138+ S7 :: method(calculate ,segment_abc ) <- function (x ){
139+
140+ out <- x @ fn @ fn_exec(x ) | >
141+ dplyr :: arrange(row_id )
142+
143+ return (out )
144+
145+ }
146+
147+
174148# ' @title complete_calendar
175149# ' @name complete_calendar
176150# ' @param x ti object
@@ -299,7 +273,7 @@ print_next_steps()
299273# ' @return segment object
300274# ' @keywords internal
301275# '
302- S7 :: method(print ,segment ) <- function (x ,... ){
276+ S7 :: method(print ,segment_abc ) <- function (x ,... ){
303277
304278
305279 n_values_len <- length(x @ category @ category_values )
@@ -309,7 +283,6 @@ S7::method(print,segment) <- function(x,...){
309283 # ## Category Values information
310284 cli :: cli_h2(" Category Information" )
311285
312- if (x @ fn @ fn_name == " abc" ){
313286
314287 if (x @ value @ value_vec == " n" ){
315288
@@ -327,7 +300,7 @@ S7::method(print,segment) <- function(x,...){
327300
328301 )
329302
330- }else {
303+ }else {
331304
332305 cli :: cat_bullet(
333306 paste(
@@ -353,9 +326,38 @@ S7::method(print,segment) <- function(x,...){
353326 ,cli :: col_br_blue(stringr :: str_flatten_comma(x @ category @ category_names ))
354327 )
355328 )
356- }else {
329+
330+
357331 cli :: cat_line(" " )
358332
333+ print_actions_steps(x )
334+
335+ print_next_steps()
336+
337+ }
338+
339+
340+
341+ # ' Print segment objects
342+ # ' @name print
343+ # ' @param x A \code{ti} object.
344+ # ' @param ... Unused. Present for S3/S7 compatibility; additional arguments are ignored.
345+ #
346+ # '
347+ # ' @return segment object
348+ # ' @keywords internal
349+ # '
350+ S7 :: method(print ,segment_cohort ) <- function (x ,... ){
351+
352+
353+
354+ print_fn_info(x )
355+
356+ # ## Category Values information
357+ cli :: cli_h2(" Category Information" )
358+
359+ cli :: cat_line(" " )
360+
359361 cli :: cat_bullet(
360362 paste(
361363 " The data set is grouped by the"
@@ -378,15 +380,14 @@ S7::method(print,segment) <- function(x,...){
378380
379381
380382
381- # # add if condition for abc vs. cohort
382- cli :: cli_h2(" Calendar:" )
383- cli :: cat_bullet(paste(" The calendar aggregated" ,cli :: col_br_magenta(x @ datum @ date_vec )," to the" ,cli :: col_yellow(x @ time_unit @ value )," time unit" ))
384- cli :: cat_bullet(" A " ,cli :: col_br_red(x @ datum @ calendar_type )," calendar is created with " ,cli :: col_green(x @ datum @ group_count ," groups" ))
385- cli :: cat_bullet(paste(" Calendar ranges from" ,cli :: col_br_green(x @ datum @ min_date )," to" ,cli :: col_br_green(x @ datum @ max_date )))
386- cli :: cat_bullet(paste(cli :: col_blue(x @ datum @ date_missing )," days were missing and replaced with 0" ))
387- cli :: cat_bullet(" New date column " ,stringr :: str_flatten_comma(cli :: col_br_red(x @ fn @ new_date_column_name ),last = " and " )," was created from " ,cli :: col_br_magenta(x @ datum @ date_vec ))
388- cli :: cat_line(" " )
389- }
383+ # # add if condition for abc vs. cohort
384+ cli :: cli_h2(" Calendar:" )
385+ cli :: cat_bullet(paste(" The calendar aggregated" ,cli :: col_br_magenta(x @ datum @ date_vec )," to the" ,cli :: col_yellow(x @ time_unit @ value )," time unit" ))
386+ cli :: cat_bullet(" A " ,cli :: col_br_red(x @ datum @ calendar_type )," calendar is created with " ,cli :: col_green(x @ datum @ group_count ," groups" ))
387+ cli :: cat_bullet(paste(" Calendar ranges from" ,cli :: col_br_green(x @ datum @ min_date )," to" ,cli :: col_br_green(x @ datum @ max_date )))
388+ cli :: cat_bullet(paste(cli :: col_blue(x @ datum @ date_missing )," days were missing and replaced with 0" ))
389+ cli :: cat_bullet(" New date column " ,stringr :: str_flatten_comma(cli :: col_br_red(x @ fn @ new_date_column_name ),last = " and " )," was created from " ,cli :: col_br_magenta(x @ datum @ date_vec ))
390+ cli :: cat_line(" " )
390391
391392 cli :: cat_line(" " )
392393
@@ -395,4 +396,3 @@ S7::method(print,segment) <- function(x,...){
395396 print_next_steps()
396397
397398}
398-
0 commit comments