Skip to content

Commit

Permalink
refactor: closes #93
Browse files Browse the repository at this point in the history
  • Loading branch information
aravindhebbali committed Jul 31, 2024
1 parent 64ba5d1 commit 9ae5580
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 213 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Imports:
dplyr,
ggplot2,
magrittr,
plotly,
RColorBrewer,
rlang,
scales,
Expand All @@ -34,14 +35,12 @@ Suggests:
gifski,
kableExtra,
knitr,
png,
plotly,
rmarkdown,
rmdformats,
testthat (>= 3.0.0),
vdiffr
VignetteBuilder: knitr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Config/testthat/edition: 3
68 changes: 6 additions & 62 deletions R/rfm-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,8 @@ rfm_plot_heatmap <- function(data, brewer_n = 5, brewer_name = "PuBu",
}

if (interactive) {
pkg_flag <- requireNamespace("plotly", quietly = TRUE)
if (pkg_flag) {
p <- rfm_plotly_heatmap(mapdata, plot_title, xaxis_label, yaxis_label, brewer_n,
brewer_name, legend_title)
} else {
if (interactive()) {
message('`plotly` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("plotly")
p <- rfm_plotly_heatmap(mapdata, plot_title, xaxis_label, yaxis_label, brewer_n,
brewer_name, legend_title)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plotly` is not installed. Using `ggplot2` instead to generate the plot!")
p <- rfm_gg_heatmap(mapdata, plot_title, xaxis_label, yaxis_label, brewer_n,
p <- rfm_plotly_heatmap(mapdata, plot_title, xaxis_label, yaxis_label, brewer_n,
brewer_name, legend_title)
}
}
} else {
p <- rfm_gg_heatmap(mapdata, plot_title, xaxis_label, yaxis_label, brewer_n,
brewer_name, legend_title)
Expand Down Expand Up @@ -216,24 +198,7 @@ rfm_plot_histogram <- function(rfm_table, metric = "recency",
names(data) <- c("score")

if (interactive) {
pkg_flag <- requireNamespace("plotly", quietly = TRUE)
if (pkg_flag) {
p <- rfm_plotly_hist(data, hist_color, plot_title, xaxis_label, yaxis_label)
} else {
if (interactive()) {
message('`plotly` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("plotly")
p <- rfm_plotly_hist(data, hist_color, plot_title, xaxis_label, yaxis_label)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plotly` is not installed. Using `ggplot2` instead to generate the plot!")
p <- rfm_gg_hist(data, hist_bins, hist_color, plot_title, xaxis_label,
yaxis_label)
}
}
p <- rfm_plotly_hist(data, hist_color, plot_title, xaxis_label, yaxis_label)
} else {
p <- rfm_gg_hist(data, hist_bins, hist_color, plot_title, xaxis_label,
yaxis_label)
Expand Down Expand Up @@ -448,32 +413,11 @@ rfm_plot_order_dist <- function(rfm_table, flip = FALSE, bar_color = NULL,
ylim_max <- rfm_order_dist_ylim(data)

if (interactive) {
pkg_flag <- requireNamespace("plotly", quietly = TRUE)
if (pkg_flag) {
colnames(data) <- c("transaction_count", "n")
rfm_plotly_order_dist(data, flip = flip, bar_color = bar_color,
plot_title = plot_title, xaxis_label = xaxis_label,
yaxis_label = yaxis_label)
} else {
if (interactive()) {
message('`plotly` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("plotly")
colnames(data) <- c("transaction_count", "n")
rfm_plotly_order_dist(data, flip = flip, bar_color = bar_color,
plot_title = plot_title, xaxis_label = xaxis_label,
yaxis_label = yaxis_label)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plotly` is not installed. Using `ggplot2` instead to generate the plot!")
p <- rfm_gg_order_dist(data, flip, bar_color, plot_title, xaxis_label,
yaxis_label, ylim_max, bar_labels, bar_label_size)
}
}
colnames(data) <- c("transaction_count", "n")
rfm_plotly_order_dist(data, flip = flip, bar_color = bar_color,
plot_title = plot_title, xaxis_label = xaxis_label,
yaxis_label = yaxis_label)
} else {

if (animate) {
pkg_flag <- requireNamespace("gganimate", quietly = TRUE)
if (pkg_flag) {
Expand Down
161 changes: 13 additions & 148 deletions R/rfm-segment.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,28 +254,8 @@ rfm_plot_segment_summary <- function(x, metric = NULL, sort = FALSE,
ceiling(.)

if (interactive) {
pkg_flag <- requireNamespace("plotly", quietly = TRUE)
if (pkg_flag) {
rfm_plotly_segment_summary(data, metric, flip, sort, ascending, bar_color,
plot_title, xaxis_label, yaxis_label)
} else {
if (interactive()) {
message('`plotly` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("plotly")
rfm_plotly_segment_summary(data, metric, flip, sort, ascending, bar_color,
plot_title, xaxis_label, yaxis_label)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plotly` is not installed. Using `ggplot2` instead to generate the plot!")
p <- rfm_gg_segment_summary(data, metric, sort, ascending, flip,
bar_color, plot_title, xaxis_label,
yaxis_label, axis_label_size, axis_label_angle,
ylim_max, bar_labels)
}
}
rfm_plotly_segment_summary(data, metric, flip, sort, ascending, bar_color,
plot_title, xaxis_label, yaxis_label)
} else {
if (animate) {
pkg_flag <- requireNamespace("gganimate", quietly = TRUE)
Expand Down Expand Up @@ -396,30 +376,9 @@ rfm_plot_revenue_dist <- function(x, flip = FALSE,
}

if (interactive) {
pkg_flag <- requireNamespace("plotly", quietly = TRUE)
if (pkg_flag) {
rfm_plotly_revenue_dist(x, flip, colors, legend_labels, plot_title,
xaxis_label, yaxis_label)
} else {
if (interactive()) {
message('`plotly` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("plotly")
rfm_plotly_revenue_dist(x, flip, colors, legend_labels, plot_title,
xaxis_label, yaxis_label)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plotly` is not installed. Using `ggplot2` instead to generate the plot!")
p <- rfm_gg_revenue_dist(x, colors, legend_labels, flip,
plot_title, xaxis_label, yaxis_label,
axis_label_size, axis_label_angle,
bar_labels, bar_label_size)
}
}
rfm_plotly_revenue_dist(x, flip, colors, legend_labels, plot_title,
xaxis_label, yaxis_label)
} else {

data <- rfm_prep_revenue_dist(x)
share_data <- data[, c("category"), drop = FALSE]

Expand Down Expand Up @@ -555,29 +514,9 @@ rfm_plot_median_recency <- function(rfm_segment_table, sort = FALSE,
data <- rfm_prep_median(rfm_segment_table, recency_days)

if (interactive) {
pkg_flag <- requireNamespace("plotly", quietly = TRUE)
if (pkg_flag) {
rfm_plotly_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label)
} else {
if (interactive()) {
message('`plotly` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("plotly")
rfm_plotly_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plotly` is not installed. Using `ggplot2` instead to generate the plot!")
p <- rfm_gg_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label, axis_label_size,
axis_label_angle, bar_labels)
}
}
rfm_plotly_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label)
} else {

if (animate) {
pkg_flag <- requireNamespace("gganimate", quietly = TRUE)
if (pkg_flag) {
Expand Down Expand Up @@ -638,29 +577,9 @@ rfm_plot_median_frequency <- function(rfm_segment_table, sort = FALSE,
data <- rfm_prep_median(rfm_segment_table, transaction_count)

if (interactive) {
pkg_flag <- requireNamespace("plotly", quietly = TRUE)
if (pkg_flag) {
rfm_plotly_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label)
} else {
if (interactive()) {
message('`plotly` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("plotly")
rfm_plotly_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plotly` is not installed. Using `ggplot2` instead to generate the plot!")
p <- rfm_gg_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label, axis_label_size,
axis_label_angle, bar_labels)
}
}
rfm_plotly_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label)
} else {

if (animate) {
pkg_flag <- requireNamespace("gganimate", quietly = TRUE)
if (pkg_flag) {
Expand Down Expand Up @@ -721,27 +640,8 @@ rfm_plot_median_monetary <- function(rfm_segment_table, sort = FALSE,
data <- rfm_prep_median(rfm_segment_table, amount)

if (interactive) {
pkg_flag <- requireNamespace("plotly", quietly = TRUE)
if (pkg_flag) {
rfm_plotly_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label)
} else {
if (interactive()) {
message('`plotly` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("plotly")
rfm_plotly_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plotly` is not installed. Using `ggplot2` instead to generate the plot!")
p <- rfm_gg_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label, axis_label_size,
axis_label_angle, bar_labels)
}
}
rfm_plotly_median(data, bar_color, sort, ascending, flip, plot_title,
xaxis_label, yaxis_label)
} else {

if (animate) {
Expand Down Expand Up @@ -848,23 +748,7 @@ rfm_plot_segment <- function(table, metric = "customers", interactive = FALSE,
table$prop <- round((table[[metric]] / sum(table[[metric]])) * 100, 2)

if (interactive) {
pkg_flag <- requireNamespace("plotly", quietly = TRUE)
if (pkg_flag) {
p <- rfm_plotly_segment(table, metric)
} else {
if (interactive()) {
message('`plotly` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("plotly")
p <- rfm_plotly_segment(table, metric)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plotly` is not installed. Using `ggplot2` instead to generate the plot!")
p <- rfm_gg_segment(table, metric)
}
}
p <- rfm_plotly_segment(table, metric)
} else {
p <- rfm_gg_segment(table, metric)
}
Expand Down Expand Up @@ -972,27 +856,8 @@ rfm_plot_segment_scatter <- function(segments, x = "monetary", y = "recency",
}

if (interactive) {
pkg_flag <- requireNamespace("plotly", quietly = TRUE)
if (pkg_flag) {
rfm_plotly_segment_scatter(segments, x_data, y_data, plot_title,
legend_title, xaxis_label, yaxis_label)
} else {
if (interactive()) {
message('`plotly` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("plotly")
rfm_plotly_segment_scatter(segments, x_data, y_data, plot_title,
legend_title, xaxis_label, yaxis_label)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plotly` is not installed. Using `ggplot2` instead to generate the plot!")
p <- rfm_gg_segment_scatter(segments, x_data, y_data, plot_title,
legend_title, xaxis_label, yaxis_label)
}
}

rfm_plotly_segment_scatter(segments, x_data, y_data, plot_title,
legend_title, xaxis_label, yaxis_label)
} else {
p <- rfm_gg_segment_scatter(segments, x_data, y_data, plot_title,
legend_title, xaxis_label, yaxis_label)
Expand Down

0 comments on commit 9ae5580

Please sign in to comment.