Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On CRAN #90

Merged
merged 18 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
^docs$
^debug\.log$
^\.github$
^revdep$
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ Suggests:
VignetteBuilder: knitr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
RoxygenNote: 7.2.3
Config/testthat/edition: 3
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export(rfm_table_order)
importFrom(RColorBrewer,brewer.pal)
importFrom(dplyr,arrange)
importFrom(dplyr,between)
importFrom(dplyr,distinct)
importFrom(dplyr,everything)
importFrom(dplyr,group_by)
importFrom(dplyr,join_by)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,n)
importFrom(dplyr,pull)
Expand Down
17 changes: 16 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# rfm 0.3.0

## Enhancements

- segment overview ([#76](https://github.com/rsquaredacademy/rfm/issues/76))
- `rfm_create_report()` generates a segmentation analysis report
- `rfm_table_customer()` handles customer level data with both recency date and days
- `interactive` argument in plotting function generates interactive plots using [plotly](https://plotly.com/r/)
- `animate` argument in plotting functions adds animation using [gganimate](https://gganimate.com)

## Bug Fixes

- incorrect package alias ([#88](https://github.com/rsquaredacademy/rfm/issues/88))
- cran checks badge ([#80](https://github.com/rsquaredacademy/rfm/issues/80))

# rfm 0.2.2

This is a patch release for an urgent bug fix.
Expand All @@ -22,7 +37,7 @@ This is a minor release for bug fixes and enhancements.

- Export plot prep data ([#20](https://github.com/rsquaredacademy/rfm/issues/20))
- Default customer segments ([#24](https://github.com/rsquaredacademy/rfm/issues/24))
- Median Recency ([#26](https://github.com/rsquaredacademy/rfm/issues/26))
- Median recency ([#26](https://github.com/rsquaredacademy/rfm/issues/26))
- Median frequency ([#27](https://github.com/rsquaredacademy/rfm/issues/27))
- Median monetary value ([#28](https://github.com/rsquaredacademy/rfm/issues/28))

Expand Down
12 changes: 9 additions & 3 deletions R/rfm-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
#'
#' @format A tibble with 49.6 rows and 3 variables:
#' \describe{
#' \item{order_date}{order date}
#' \item{customer_id}{customer id}
#' \item{revenue}{transaction amount}
#' \item{order_date}{Order date.}
#' \item{customer_id}{Customer id}
#' \item{revenue}{Transaction amount.}
#' \item{first_name}{First name of the customer.}
#' \item{last_name}{Last name of the customer.}
#' \item{email}{email id of the customer.}
#' }
#'
"rfm_data_orders"
Expand All @@ -22,6 +25,9 @@
#' \item{most_recent_visit}{Date of the most recent transaction.}
#' \item{number_of_purchases}{Total number of transactions/orders.}
#' \item{purchase_interval}{Number of days since last transaction/order.}
#' #' \item{first_name}{First name of the customer.}
#' \item{last_name}{Last name of the customer.}
#' \item{email}{email id of the customer.}
#' }
#'
"rfm_data_customer"
33 changes: 12 additions & 21 deletions R/rfm-ggplot.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @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) {
brewer_n, brewer_name, legend_title) {

ulm <- ceiling(max(mapdata[["monetary"]]))
llm <- floor(min(mapdata[["monetary"]]))
Expand All @@ -20,11 +20,9 @@ rfm_gg_heatmap <- function(mapdata, plot_title, xaxis_label, yaxis_label,
colours = brewer.pal(n = brewer_n, name = brewer_name),
name = legend_title)

if (print_plot) {
print(p)
} else {
return(p)
}

return(p)

}

#' @importFrom ggplot2 geom_bar ylim coord_flip geom_text position_dodge
Expand Down Expand Up @@ -68,7 +66,7 @@ rfm_gg_order_dist <- function(data, flip, bar_color, plot_title, xaxis_label,

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

p <-
data %>%
Expand All @@ -78,11 +76,7 @@ rfm_gg_hist <- function(data, hist_bins, hist_color, plot_title, xaxis_label,
ylab(yaxis_label) +
ggtitle(plot_title)

if (print_plot) {
print(p)
} else {
return(p)
}
return(p)

}

Expand Down Expand Up @@ -171,8 +165,8 @@ rfm_gg_revenue_dist <- function(data, colors, legend_labels, flip,

p <-
ggplot(data, aes(fill = category, y = share, x = segment)) +
geom_bar(position = "dodge", stat = "identity")
geom_bar(position = "dodge", stat = "identity")


p <-
p +
Expand All @@ -185,7 +179,7 @@ rfm_gg_revenue_dist <- function(data, colors, legend_labels, flip,
theme(legend.title = element_blank(),
legend.position = "bottom",
panel.background = element_rect(fill = NA),
axis.ticks = element_line(color = NA))
axis.ticks = element_line(color = NA))


if (flip) {
Expand Down Expand Up @@ -254,7 +248,7 @@ rfm_gg_median <- function(data, bar_color, sort, ascending, flip, plot_title,
}

if (is.null(bar_color)) {
bar_color <- "blue"
bar_color <- "#0f1a34"
}

if (sort) {
Expand Down Expand Up @@ -341,11 +335,8 @@ rfm_gg_segment <- function(table, metric, print_plot) {
geom_treemap_text(size = 8, place = 'centre') +
theme(legend.position = "none")

if (print_plot) {
print(plot)
} else {
return(plot)
}
return(plot)

}

rfm_gg_segment_scatter <- function(segments, x_data, y_data, plot_title,
Expand Down
8 changes: 4 additions & 4 deletions R/rfm-plotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,18 +327,18 @@ rfm_plotly_median <- function(data, bar_color = NULL, sort = FALSE,

if (is.null(yaxis_label)) {
yaxis_label <- ycol
}
}

if (is.null(plot_title)) {
plot_title <- paste(yaxis_label, " by Segment")
}
}

if (is.null(xaxis_label)) {
xaxis_label <- "Segment"
}
}

if (is.null(bar_color)) {
bar_color <- "blue"
bar_color <- "#0f1a34"
}

if (flip) {
Expand Down
38 changes: 22 additions & 16 deletions R/rfm-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,33 @@ rfm_plot_heatmap <- function(data, brewer_n = 5, brewer_name = "PuBu",
if (interactive) {
pkg_flag <- requireNamespace("plotly", quietly = TRUE)
if (pkg_flag) {
rfm_plotly_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 {
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_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 {
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plot` is not installed. Using `ggplot2` instead to generate the plot!")
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,
brewer_name, legend_title, print_plot)
brewer_name, legend_title)
}
}
} else {
rfm_gg_heatmap(mapdata, plot_title, xaxis_label, yaxis_label, brewer_n,
brewer_name, legend_title, print_plot)
p <- rfm_gg_heatmap(mapdata, plot_title, xaxis_label, yaxis_label, brewer_n,
brewer_name, legend_title)
}

if (print_plot) {
print(p)
} else {
return(p)
}

}
Expand Down Expand Up @@ -197,7 +203,7 @@ rfm_plot_histogram <- function(rfm_table, metric = "recency",
}

if (is.null(hist_color)) {
hist_color <- "blue"
hist_color <- "#0f1a34"
}

ycol <- switch(metric,
Expand All @@ -223,14 +229,14 @@ rfm_plot_histogram <- function(rfm_table, metric = "recency",
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plot` is not installed. Using `ggplot2` instead to generate the plot!")
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, print_plot)
yaxis_label)
}
}
} else {
p <- rfm_gg_hist(data, hist_bins, hist_color, plot_title, xaxis_label,
yaxis_label, print_plot)
yaxis_label)
}

if (print_plot) {
Expand All @@ -245,7 +251,7 @@ rfm_plot_histogram <- function(rfm_table, metric = "recency",
#' @rdname rfm_plot_histogram
#' @usage NULL
#'
rfm_histograms <- function(rfm_table, hist_bins = 9, hist_color = 'blue',
rfm_histograms <- function(rfm_table, hist_bins = 9, hist_color = "#0f1a34",
plot_title = 'RFM Histograms', xaxis_title = ' ',
yaxis_title = 'Count', print_plot = TRUE) {
.Deprecated("rfm_plot_histograms()")
Expand Down Expand Up @@ -299,7 +305,7 @@ rfm_plot_bar_chart <- function(rfm_table, bar_color = NULL,
print_plot = TRUE) {

if (is.null(bar_color)) {
bar_color <- "blue"
bar_color <- "#0f1a34"
}

if (is.null(xaxis_label)) {
Expand Down Expand Up @@ -343,7 +349,7 @@ rfm_plot_bar_chart <- function(rfm_table, bar_color = NULL,
#' @rdname rfm_plot_bar_chart
#' @usage NULL
#'
rfm_bar_chart <- function(rfm_table, bar_color = 'blue',
rfm_bar_chart <- function(rfm_table, bar_color = '#0f1a34',
xaxis_title = 'Monetary Score',
sec_xaxis_title = 'Frequency Score',
yaxis_title = ' ',
Expand Down Expand Up @@ -431,7 +437,7 @@ rfm_plot_order_dist <- function(rfm_table, flip = FALSE, bar_color = NULL,
}

if (is.null(bar_color)) {
bar_color <- "blue"
bar_color <- "#0f1a34"
}

if (interactive) {
Expand Down Expand Up @@ -461,7 +467,7 @@ rfm_plot_order_dist <- function(rfm_table, flip = FALSE, bar_color = NULL,
stop('Sorry! The functionality is not available without installing the required package.', call. = FALSE)
}
} else {
warning("`plot` is not installed. Using `ggplot2` instead to generate the plot!")
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)
}
Expand Down Expand Up @@ -512,7 +518,7 @@ rfm_plot_order_dist <- function(rfm_table, flip = FALSE, bar_color = NULL,
#' @rdname rfm_plot_order_dist
#' @usage NULL
#'
rfm_order_dist <- function(rfm_table, bar_color = 'blue',
rfm_order_dist <- function(rfm_table, bar_color = '#0f1a34',
xaxis_title = 'Orders', yaxis_title = 'Customers',
plot_title = 'Customers by Orders',
print_plot = TRUE) {
Expand Down
4 changes: 2 additions & 2 deletions R/rfm-report.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' RFM report
#'
#' Generates a segmentation ananlysis report.
#' Generates a segmentation analysis report.
#'
#' @param rfm_table An object of class \code{rfm_table}.
#' @param segments Output from \code{rfm_segment}.
Expand Down Expand Up @@ -41,7 +41,7 @@
#' rfm_create_report(rfm_result, segments, FALSE,
#' "Customer Segmentation Report")
#' }
#'
#'
#' @importFrom utils browseURL
#'
#' @export
Expand Down
Loading
Loading