Skip to content

Commit

Permalink
refactor: non standard evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
aravindhebbali committed Jun 27, 2023
1 parent 7d6275c commit b36e51c
Show file tree
Hide file tree
Showing 34 changed files with 986 additions and 936 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ BugReports: https://github.com/rsquaredacademy/rfm/issues
Depends:
R (>= 3.2)
Imports:
data.table,
dplyr,
gganimate,
ggplot2,
gifski,
magrittr,
plotly,
RColorBrewer,
rlang,
scales,
stats,
treemapify,
Expand All @@ -31,8 +31,10 @@ Imports:
Suggests:
covr,
DT,
gifski,
kableExtra,
knitr,
png,
rmarkdown,
testthat,
vdiffr
Expand Down
51 changes: 45 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,59 @@ export(rfm_segment_summary)
export(rfm_table_customer)
export(rfm_table_customer_2)
export(rfm_table_order)
import(data.table)
import(gganimate)
import(ggplot2)
import(gifski)
import(magrittr)
import(treemapify)
importFrom(RColorBrewer,brewer.pal)
importFrom(dplyr,group_by)
importFrom(dplyr,mutate)
importFrom(dplyr,n)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(gganimate,animate)
importFrom(gganimate,ease_aes)
importFrom(gganimate,gifski_renderer)
importFrom(gganimate,transition_reveal)
importFrom(gganimate,transition_states)
importFrom(ggplot2,aes)
importFrom(ggplot2,coord_flip)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_line)
importFrom(ggplot2,element_rect)
importFrom(ggplot2,element_text)
importFrom(ggplot2,facet_grid)
importFrom(ggplot2,geom_bar)
importFrom(ggplot2,geom_histogram)
importFrom(ggplot2,geom_point)
importFrom(ggplot2,geom_text)
importFrom(ggplot2,geom_tile)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,ggtitle)
importFrom(ggplot2,labs)
importFrom(ggplot2,position_dodge)
importFrom(ggplot2,scale_fill_gradientn)
importFrom(ggplot2,scale_fill_manual)
importFrom(ggplot2,scale_y_continuous)
importFrom(ggplot2,sec_axis)
importFrom(ggplot2,theme)
importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(ggplot2,ylim)
importFrom(magrittr,"%<>%")
importFrom(magrittr,"%>%")
importFrom(magrittr,extract)
importFrom(magrittr,multiply_by)
importFrom(magrittr,set_names)
importFrom(magrittr,use_series)
importFrom(plotly,add_trace)
importFrom(plotly,config)
importFrom(plotly,layout)
importFrom(plotly,plot_ly)
importFrom(rlang,int)
importFrom(stats,median)
importFrom(stats,quantile)
importFrom(stats,reorder)
importFrom(stats,runif)
importFrom(treemapify,geom_treemap)
importFrom(treemapify,geom_treemap_text)
importFrom(utils,available.packages)
importFrom(utils,install.packages)
importFrom(utils,menu)
Expand Down
1 change: 1 addition & 0 deletions R/rfm-animate.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rfm_animate_data <- function(data, metric) {
result
}

#' @importFrom gganimate transition_states ease_aes
rfm_animate_plot <- function(p) {
p +
transition_states(
Expand Down
40 changes: 24 additions & 16 deletions R/rfm-ggplot.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#' @importFrom ggplot2 ggplot geom_tile aes ggtitle xlab ylab scale_fill_gradientn
#' @importFrom RColorBrewer brewer.pal
rfm_gg_heatmap <- function(mapdata, plot_title, xaxis_label, yaxis_label,
brewer_n, brewer_name, legend_title, print_plot) {

Expand All @@ -15,9 +17,8 @@ rfm_gg_heatmap <- function(mapdata, plot_title, xaxis_label, yaxis_label,
xlab(xaxis_label) +
ylab(yaxis_label) +
scale_fill_gradientn(limits = c(llm, ulm),
colours = RColorBrewer::brewer.pal(n = brewer_n,
name = brewer_name),
name = legend_title)
colours = brewer.pal(n = brewer_n, name = brewer_name),
name = legend_title)

if (print_plot) {
print(p)
Expand All @@ -26,6 +27,7 @@ rfm_gg_heatmap <- function(mapdata, plot_title, xaxis_label, yaxis_label,
}
}

#' @importFrom ggplot2 geom_bar ylim coord_flip geom_text position_dodge
rfm_gg_order_dist <- function(data, flip, bar_color, plot_title, xaxis_label,
yaxis_label, ylim_max, bar_labels, bar_label_size) {

Expand Down Expand Up @@ -64,6 +66,7 @@ rfm_gg_order_dist <- function(data, flip, bar_color, plot_title, xaxis_label,
return(p)
}

#' @importFrom ggplot2 geom_histogram
rfm_gg_hist <- function(data, hist_bins, hist_color, plot_title, xaxis_label,
yaxis_label, print_plot) {

Expand All @@ -83,9 +86,10 @@ rfm_gg_hist <- function(data, hist_bins, hist_color, plot_title, xaxis_label,

}

#' @importFrom ggplot2 theme element_text
rfm_gg_segment_summary <- function(data, metric, sort, ascending, flip,
bar_color, plot_title, xaxis_label,
yaxis_label, axis_label_size,
yaxis_label, axis_label_size,
axis_label_angle, ylim_max, bar_labels) {

if (sort) {
Expand Down Expand Up @@ -157,9 +161,11 @@ rfm_gg_segment_summary <- function(data, metric, sort, ascending, flip,

}

rfm_gg_revenue_dist <- function(data, colors, legend_labels, flip,
plot_title, xaxis_label, yaxis_label,
axis_label_size, axis_label_angle,
#' @importFrom ggplot2 scale_fill_manual scale_y_continuous element_blank
#' @importFrom ggplot2 element_line element_rect
rfm_gg_revenue_dist <- function(data, colors, legend_labels, flip,
plot_title, xaxis_label, yaxis_label,
axis_label_size, axis_label_angle,
bar_labels, bar_labels_size) {

ylim_max <-
Expand Down Expand Up @@ -229,7 +235,7 @@ rfm_gg_revenue_dist <- function(data, colors, legend_labels, flip,
}

rfm_gg_median <- function(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label, axis_label_size, axis_label_angle,
xaxis_label, yaxis_label, axis_label_size, axis_label_angle,
bar_labels) {

n_fill <- nrow(data)
Expand Down Expand Up @@ -308,7 +314,7 @@ rfm_gg_median <- function(data, bar_color, sort, ascending, flip, plot_title,
} else {
p <-
p +
theme(axis.text.x = element_text(angle = axis_label_angle,
theme(axis.text.x = element_text(angle = axis_label_angle,
size = axis_label_size,
hjust = 0))

Expand All @@ -327,19 +333,20 @@ rfm_gg_median <- function(data, bar_color, sort, ascending, flip, plot_title,

}

#' @importFrom treemapify geom_treemap geom_treemap_text
rfm_gg_segment <- function(table, metric, print_plot) {

plot <- ggplot(table,
aes(area = .data[[metric]],
fill = segment,
label = paste(toupper(segment),
paste0(.data[[metric]], " (", prop, "%)"),
sep = '\n'))) +
aes(area = .data[[metric]],
fill = segment,
label = paste(toupper(segment),
paste0(.data[[metric]], " (", prop, "%)"),
sep = '\n'))) +
geom_treemap() +
geom_treemap_text(size = 8, place = 'centre') +
theme(legend.position = "none")

if (print_plot) {
if (print_plot) {
print(plot)
} else {
return(plot)
Expand All @@ -350,10 +357,11 @@ rfm_gg_segment_scatter <- function(segments, x_data, y_data, plot_title,
legend_title, xaxis_label, yaxis_label) {

rfm_gg_combine(segments, x_data, y_data, xaxis_label, yaxis_label,
plot_title, legend_title)
plot_title, legend_title)

}

#' @importFrom ggplot2 geom_point labs
rfm_gg_combine <- function(rfm_table, x = "amount", y = "recency_days",
xaxis_title = "Monetary", yaxis_title = "Recency",
plot_title = "Recency vs Monetary",
Expand Down
49 changes: 22 additions & 27 deletions R/rfm-plots-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@
#' # heat map data
#' rfm_heatmap_data(rfm_customer)
#'
#' @importFrom magrittr use_series
#' @importFrom dplyr group_by summarise
#'
#' @export
#'
rfm_heatmap_data <- function(rfm_table) {

result <-
rfm_table %>%
use_series(rfm) %>%
data.table() %>%
.[, .(frequency_score, recency_score, amount)] %>%
.[, .(monetary = mean(amount)),
keyby = .(frequency_score, recency_score)] %>%
setDF()

l_frequency <- check_levels(result, frequency_score)
l_recency <- check_levels(result, recency_score)
levels_frequency <- check_levels(result, frequency_score) %>% length()
levels_recency <- check_levels(result, recency_score) %>% length()
group_by(frequency_score, recency_score) %>%
select(frequency_score, recency_score, amount) %>%
summarise(monetary = mean(amount))

l_frequency <- check_levels(result, "frequency_score")
l_recency <- check_levels(result, "recency_score")
levels_frequency <- length(l_frequency)
levels_recency <- length(l_recency)
f_frequency <- use_series(rfm_table, frequency_bins)
r_recency <- use_series(rfm_table, recency_bins)

Expand Down Expand Up @@ -95,15 +96,16 @@ rfm_barchart_data <- function(rfm_table) {

}

#' @importFrom dplyr rename
rfm_order_dist_data <- function(rfm_table) {
rfm_table %>%
rfm_result %>%
use_series(rfm) %>%
data.table() %>%
.[, .(n = .N), by = transaction_count] %>%
setnames(old = "transaction_count", new = "segment") %>%
setDF()
group_by(transaction_count) %>%
summarise(n = n()) %>%
rename(segment = transaction_count)
}

#' @importFrom magrittr multiply_by
rfm_order_dist_ylim <- function(data) {
data %>%
use_series(n) %>%
Expand Down Expand Up @@ -136,17 +138,10 @@ rfm_prep_revenue_dist <- function(x) {

rfm_prep_median <- function(rfm_segment_table, metric) {

met <- deparse(substitute(metric))

result <-
rfm_segment_table %>%
data.table() %>%
.[, .(segment, met = get(met))] %>%
.[, .(mem = median(met)), by = segment] %>%
.[order(mem)] %>%
setnames(old = "mem", new = met) %>%
setDF()

return(result)
rfm_segment_table %>%
select(segment, {{ metric }}) %>%
group_by(segment) %>%
summarize({{ metric }} := median({{ metric }})) %>%
arrange({{ metric }})

}
4 changes: 3 additions & 1 deletion R/rfm-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ rfm_histograms <- function(rfm_table, hist_bins = 9, hist_color = 'blue',
#'
#' # bar chart
#' rfm_plot_bar_chart(rfm_order)
#'
#' @importFrom ggplot2 sec_axis facet_grid
#'
#' @export
#'
Expand Down Expand Up @@ -367,7 +369,7 @@ rfm_bar_chart <- function(rfm_table, bar_color = 'blue',
#' # order distribution
#' rfm_plot_order_dist(rfm_customer)
#'
#' @import gganimate gifski
#' @importFrom gganimate animate gifski_renderer
#'
#' @export
#'
Expand Down
24 changes: 11 additions & 13 deletions R/rfm-segment.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,14 @@ rfm_segment <- function(data, segment_names = NULL, recency_lower = NULL,
#'
rfm_segment_summary <- function(segments) {

result <-
segments %>%
data.table() %>%
.[, .(customers = .N,
orders = sum(transaction_count),
revenue = sum(amount)),
by = segment] %>%
setDF()

result$aov <- result$revenue / result$orders
return(result)
segments %>%
group_by(segment) %>%
summarise(
customers = n(),
orders = sum(transaction_count),
revenue = sum(amount)
) %>%
mutate(aov = revenue / orders)

}

Expand Down Expand Up @@ -555,7 +552,7 @@ rfm_plot_median_monetary <- function(rfm_segment_table, sort = FALSE,
}

p <- rfm_gg_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label, axis_label_size, axis_label_angle,
xaxis_label, yaxis_label, axis_label_size, axis_label_angle,
bar_labels)

if (animate) {
Expand Down Expand Up @@ -628,7 +625,6 @@ rfm_plot_median_monetary <- function(rfm_segment_table, sort = FALSE,
#' # plotly
#' rfm_plot_segment(segment_overview, metric = "revenue", interactive = TRUE)
#'
#' @import treemapify
#' @export
#'
rfm_plot_segment <- function(table, metric = "customers", interactive = FALSE,
Expand Down Expand Up @@ -697,6 +693,8 @@ rfm_plot_segment <- function(table, metric = "customers", interactive = FALSE,
#' # plotly
#' rfm_plot_segment_scatter(segments, "monetary", "recency", interactive = TRUE)
#'
#' @importFrom gganimate transition_reveal
#'
#' @export
rfm_plot_segment_scatter <- function(segments, x = "monetary", y = "recency",
plot_title = NULL, legend_title = NULL,
Expand Down
Loading

0 comments on commit b36e51c

Please sign in to comment.