Skip to content

Commit 941a092

Browse files
committed
segment_abc and segment_cohort
1 parent c8fbb1d commit 941a092

File tree

6 files changed

+104
-71
lines changed

6 files changed

+104
-71
lines changed

R/abc.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#' - If you do not provide a `.value` then it will count the transactions per group, if you provide `.value` then it will [sum()] the `.value` per group
1515
#' - The function creates a `segment` object, which pre-processes the data into its components
1616
#'
17-
#' @returns segment object
17+
#' @returns abc object
1818
#' @export
1919
#'
2020
#' @examples
@@ -41,7 +41,7 @@ abc <- function(.data,category_values,.value){
4141
}
4242

4343

44-
x <- segment(
44+
x <- segment_abc(
4545
datum = datum(.data,date_vec = NA,calendar_type = NA)
4646
,value = value(value_vec = value_vec,new_column_name_vec = "abc")
4747
,category = category(category_values=category_values)
@@ -226,13 +226,12 @@ cohort <- function(.data,.date,.value,calendar_type,time_unit="month",period_lab
226226
# calendar_type <- "standard"
227227

228228

229-
x <- segment(
229+
x <- segment_cohort(
230230
datum= datum(
231231
.data
232232
,calendar_type = calendar_type
233233
,date_vec = rlang::as_label(rlang::enquo(.date))
234234
)
235-
,category = category(category_values = 0)
236235
,fn = fn(
237236
fn_exec = cohort_fn
238237
,fn_name = "cohort"

R/classes.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,10 @@ category <- S7::new_class(
370370
)
371371
)
372372

373-
segment <- S7::new_class(
374373

375-
,name="segment"
374+
segment_abc <- S7::new_class(
375+
376+
,name="segment_abc"
376377
,package = "fpaR"
377378
,properties = list(
378379
datum=datum
@@ -383,3 +384,18 @@ segment <- S7::new_class(
383384
,value=value
384385
)
385386
)
387+
388+
389+
390+
segment_cohort <- S7::new_class(
391+
392+
,name="segment_cohort"
393+
,package = "fpaR"
394+
,properties = list(
395+
datum=datum
396+
,time_unit=time_unit
397+
,fn=fn
398+
,action=action
399+
,value=value
400+
)
401+
)

R/methods.R

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -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-

man/abc.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/calculate.Rd

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

man/print.Rd

Lines changed: 8 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)