Skip to content

Commit

Permalink
refactor: closes #94
Browse files Browse the repository at this point in the history
  • Loading branch information
aravindhebbali committed Jul 31, 2024
1 parent 9ae5580 commit f298570
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 148 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ Depends:
R (>= 3.2)
Imports:
dplyr,
gganimate,
ggplot2,
gifski,
magrittr,
plotly,
RColorBrewer,
Expand All @@ -31,8 +33,6 @@ Suggests:
cli,
covr,
DT,
gganimate,
gifski,
kableExtra,
knitr,
rmarkdown,
Expand Down
21 changes: 2 additions & 19 deletions R/rfm-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -419,25 +419,8 @@ rfm_plot_order_dist <- function(rfm_table, flip = FALSE, bar_color = NULL,
yaxis_label = yaxis_label)
} else {
if (animate) {
pkg_flag <- requireNamespace("gganimate", quietly = TRUE)
if (pkg_flag) {
print_plot <- FALSE
data <- rfm_animate_data(data, "n")
} else {
if (interactive()) {
message('`gganimate` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("gganimate")
print_plot <- FALSE
data <- rfm_animate_data(data, "n")
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
animate <- FALSE
warning("`gganimate` is not installed. Using `ggplot2` instead to generate the plot!")
}
}
print_plot <- FALSE
data <- rfm_animate_data(data, "n")
}

p <- rfm_gg_order_dist(data, flip, bar_color, plot_title, xaxis_label,
Expand Down
146 changes: 19 additions & 127 deletions R/rfm-segment.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,25 +258,8 @@ rfm_plot_segment_summary <- function(x, metric = NULL, sort = FALSE,
plot_title, xaxis_label, yaxis_label)
} else {
if (animate) {
pkg_flag <- requireNamespace("gganimate", quietly = TRUE)
if (pkg_flag) {
print_plot <- FALSE
data <- rfm_animate_data(data, metric)
} else {
if (interactive()) {
message('`gganimate` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("gganimate")
print_plot <- FALSE
data <- rfm_animate_data(data, metric)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
animate <- FALSE
warning("`gganimate` is not installed. Using `ggplot2` instead to generate the plot!")
}
}
print_plot <- FALSE
data <- rfm_animate_data(data, metric)
}

p <- rfm_gg_segment_summary(data, metric, sort, ascending, flip,
Expand All @@ -286,7 +269,7 @@ rfm_plot_segment_summary <- function(x, metric = NULL, sort = FALSE,

if (animate) {
p <- rfm_animate_plot(p)
gganimate::animate(p, fps=8, renderer = gganimate::gifski_renderer(loop = FALSE))
gganimate::animate(p, fps = 8, renderer = gganimate::gifski_renderer(loop = FALSE))
}

if (print_plot) {
Expand Down Expand Up @@ -383,29 +366,10 @@ rfm_plot_revenue_dist <- function(x, flip = FALSE,
share_data <- data[, c("category"), drop = FALSE]

if (animate) {
pkg_flag <- requireNamespace("gganimate", quietly = TRUE)
if (pkg_flag) {
print_plot <- FALSE
bar_labels <- FALSE
data <- rfm_animate_data(data, "share")
data$category <- rep(share_data$category, 10)
} else {
if (interactive()) {
message('`gganimate` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("gganimate")
print_plot <- FALSE
bar_labels <- FALSE
data <- rfm_animate_data(data, "share")
data$category <- rep(share_data$category, 10)
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
animate <- FALSE
warning("`gganimate` is not installed. Using `ggplot2` instead to generate the plot!")
}
}
print_plot <- FALSE
bar_labels <- FALSE
data <- rfm_animate_data(data, "share")
data$category <- rep(share_data$category, 10)
}

p <- rfm_gg_revenue_dist(data, colors, legend_labels, flip,
Expand Down Expand Up @@ -518,25 +482,8 @@ rfm_plot_median_recency <- function(rfm_segment_table, sort = FALSE,
xaxis_label, yaxis_label)
} else {
if (animate) {
pkg_flag <- requireNamespace("gganimate", quietly = TRUE)
if (pkg_flag) {
print_plot <- FALSE
data <- rfm_animate_data(data, "recency_days")
} else {
if (interactive()) {
message('`gganimate` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("gganimate")
print_plot <- FALSE
data <- rfm_animate_data(data, "recency_days")
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
animate <- FALSE
warning("`gganimate` is not installed. Using `ggplot2` instead to generate the plot!")
}
}
print_plot <- FALSE
data <- rfm_animate_data(data, "recency_days")
}

p <- rfm_gg_median(data, bar_color, sort, ascending, flip, plot_title,
Expand Down Expand Up @@ -581,25 +528,8 @@ rfm_plot_median_frequency <- function(rfm_segment_table, sort = FALSE,
xaxis_label, yaxis_label)
} else {
if (animate) {
pkg_flag <- requireNamespace("gganimate", quietly = TRUE)
if (pkg_flag) {
print_plot <- FALSE
data <- rfm_animate_data(data, "transaction_count")
} else {
if (interactive()) {
message('`gganimate` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("gganimate")
print_plot <- FALSE
data <- rfm_animate_data(data, "transaction_count")
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
animate <- FALSE
warning("`gganimate` is not installed. Using `ggplot2` instead to generate the plot!")
}
}
print_plot <- FALSE
data <- rfm_animate_data(data, "transaction_count")
}

p <- rfm_gg_median(data, bar_color, sort, ascending, flip, plot_title,
Expand Down Expand Up @@ -645,25 +575,8 @@ rfm_plot_median_monetary <- function(rfm_segment_table, sort = FALSE,
} else {

if (animate) {
pkg_flag <- requireNamespace("gganimate", quietly = TRUE)
if (pkg_flag) {
print_plot <- FALSE
data <- rfm_animate_data(data, "amount")
} else {
if (interactive()) {
message('`gganimate` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("gganimate")
print_plot <- FALSE
data <- rfm_animate_data(data, "amount")
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
animate <- FALSE
warning("`gganimate` is not installed. Using `ggplot2` instead to generate the plot!")
}
}
print_plot <- FALSE
data <- rfm_animate_data(data, "amount")
}

p <- rfm_gg_median(data, bar_color, sort, ascending, flip, plot_title,
Expand Down Expand Up @@ -863,33 +776,12 @@ rfm_plot_segment_scatter <- function(segments, x = "monetary", y = "recency",
legend_title, xaxis_label, yaxis_label)

if (animate) {
pkg_flag <- requireNamespace("gganimate", quietly = TRUE)
if (pkg_flag) {
print_plot <- FALSE
p <-
p +
gganimate::transition_reveal(along = rfm_score)

gganimate::animate(p, fps=8, renderer = gganimate::gifski_renderer(loop = FALSE))
} else {
if (interactive()) {
message('`gganimate` must be installed for this functionality. Would you like to install?')
if (menu(c("Yes", "No")) == 1) {
install.packages("gganimate")
print_plot <- FALSE
p <-
p +
gganimate::transition_reveal(along = rfm_score)

gganimate::animate(p, fps=8, renderer = gganimate::gifski_renderer(loop = FALSE))
} else {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
animate <- FALSE
warning("`gganimate` is not installed. Using `ggplot2` instead to generate the plot!")
}
}
print_plot <- FALSE
p <-
p +
gganimate::transition_reveal(along = rfm_score)

gganimate::animate(p, fps=8, renderer = gganimate::gifski_renderer(loop = FALSE))
}

if (print_plot) {
Expand Down

0 comments on commit f298570

Please sign in to comment.