Skip to content

Commit

Permalink
trend narrative fixed for time periods and shorter time series
Browse files Browse the repository at this point in the history
  • Loading branch information
denisabd committed Jul 3, 2023
1 parent 24914fb commit 1101eed
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 118 deletions.
13 changes: 12 additions & 1 deletion R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ pytd_volume <- function(
previous_year <- lubridate::year(py_date)
}

df <- df %>%
dplyr::mutate(year = lubridate::year(base::get(date))) %>%
dplyr::filter(year == previous_year,
base::get(date) <= py_date)

# return NA rather than 0 if there are no records for previous year
if (nrow(df) == 0) return(NA)

py_volume <- df %>%
dplyr::mutate(year = lubridate::year(base::get(date))) %>%
dplyr::filter(year == previous_year,
Expand Down Expand Up @@ -413,7 +421,10 @@ get_trend_outliers <- function(
) %>%
dplyr::ungroup() %>%
dplyr::select(-data) %>%
dplyr::arrange(-abs_change)
dplyr::arrange(-abs_change) %>%
dplyr::filter(!is.na(change))

if (nrow(table) == 0) return(NULL)

if (summarization %in% c("sum", "count")) {

Expand Down
131 changes: 70 additions & 61 deletions R/narrate_trend.R
Original file line number Diff line number Diff line change
Expand Up @@ -241,76 +241,83 @@ narrate_trend <- function(
narrative_name <- glue::glue("{timeframe_curr} vs {timeframe_prev}")

change <- round(total_curr_raw - total_prev_raw, 1)
change_p <- paste(round((total_curr_raw/total_prev_raw - 1)*100, 1), "%")
trend <- ifelse(change > 0, "increase", "decrease")

if (format_numbers == TRUE) {
total_curr <- format_num(total_curr_raw, decimals = 2)
total_prev <- format_num(total_prev_raw, decimals = 2)
change <- format_num(change, decimals = 2)
} else {
total_curr <- total_curr_raw
total_prev <- total_prev_raw
change <- format_num(change, decimals = 2)
}
if (!is.na(change)) {

# Sum/Count ---------------------------------------------------------------
if (summarization %in% c("sum", "count")) {
change_p <- paste(round((total_curr_raw/total_prev_raw - 1)*100, 1), "%")
trend <- ifelse(change > 0, "increase", "decrease")

narrative_total <- glue::glue(
template_total,
.transformer = collapse_transformer(sep = collapse_sep, last = collapse_last)
)
if (format_numbers == TRUE) {
total_curr <- format_num(total_curr_raw, decimals = 2)
total_prev <- format_num(total_prev_raw, decimals = 2)
change <- format_num(change, decimals = 2)
} else {
total_curr <- total_curr_raw
total_prev <- total_prev_raw
change <- format_num(change, decimals = 2)
}

# Sum/Count ---------------------------------------------------------------
if (summarization %in% c("sum", "count")) {

narrative <- list(narrative_total) %>%
rlang::set_names(narrative_name)

variables <- list(
list(
narrative_total = narrative_total,
template_total = template_total,
measure = measure,
total_curr = total_curr,
total_curr_raw = total_curr_raw,
total_prev = total_prev,
total_prev_raw = total_prev_raw,
timeframe_curr = timeframe_curr,
timeframe_prev = timeframe_prev,
change = change,
change_p = change_p,
trend = trend
narrative_total <- glue::glue(
template_total,
.transformer = collapse_transformer(sep = collapse_sep, last = collapse_last)
)
) %>%
rlang::set_names(narrative_name)

# Average ------------------------------------------------------------
} else if (summarization == "average") {
narrative <- list(narrative_total) %>%
rlang::set_names(narrative_name)

narrative_average <- glue::glue(
template_average,
.transformer = collapse_transformer(sep = collapse_sep, last = collapse_last)
)
variables <- list(
list(
narrative_total = narrative_total,
template_total = template_total,
measure = measure,
total_curr = total_curr,
total_curr_raw = total_curr_raw,
total_prev = total_prev,
total_prev_raw = total_prev_raw,
timeframe_curr = timeframe_curr,
timeframe_prev = timeframe_prev,
change = change,
change_p = change_p,
trend = trend
)
) %>%
rlang::set_names(narrative_name)

narrative <- list(narrative_average) %>%
rlang::set_names(narrative_name)

variables <- list(
list(
narrative_average = narrative_average,
template_average = template_average,
measure = measure,
total_curr = total_curr,
total_curr_raw = total_curr_raw,
total_prev = total_prev,
total_prev_raw = total_prev_raw,
timeframe_curr = timeframe_curr,
timeframe_prev = timeframe_prev,
change = change,
change_p = change_p,
trend = trend
# Average ------------------------------------------------------------
} else if (summarization == "average") {

narrative_average <- glue::glue(
template_average,
.transformer = collapse_transformer(sep = collapse_sep, last = collapse_last)
)
) %>%
rlang::set_names(narrative_name)

narrative <- list(narrative_average) %>%
rlang::set_names(narrative_name)

variables <- list(
list(
narrative_average = narrative_average,
template_average = template_average,
measure = measure,
total_curr = total_curr,
total_curr_raw = total_curr_raw,
total_prev = total_prev,
total_prev_raw = total_prev_raw,
timeframe_curr = timeframe_curr,
timeframe_prev = timeframe_prev,
change = change,
change_p = change_p,
trend = trend
)
) %>%
rlang::set_names(narrative_name)
}
} else {
narrative <- list()
variables <- list()
}

# Adding Date as dimension and leaving last two years only
Expand Down Expand Up @@ -558,6 +565,8 @@ narrate_trend <- function(
}

# Output ------------------------------------------------------------------
if (length(narrative) == 0) return(NULL)

if (return_data == TRUE) {
return(variables)
}
Expand Down
46 changes: 0 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,6 @@ narrative_enhanced <- enhance_narrative(narrative_one)
cat(narrative_enhanced)
```

The Total Sales generated across all Regions amounted to a staggering
38790478.4. Upon closer examination, it was found that the Outlying
Regions, namely NA and EMEA, contributed significantly to this
impressive figure. NA accounted for 18079736.4, which translates to
46.6%, while EMEA contributed 13555412.7, which is 34.9% of the Total
Sales.

Further analysis of the sales data revealed that within NA, the
significant Products that contributed to these sales figures were Food &
Beverage, generating 7392821 or 40.9%, followed by Electronics, which
generated 3789132.7 or 21%. Similarly, within EMEA, the significant
Products were Food & Beverage, generating 5265113.2 or 38.8%, followed
by Electronics, which generated 3182803.4 or 23.5%.

It is noteworthy to mention that the Outlying Products that contributed
to the Total Sales were primarily Food & Beverage, generating 15543469.7
or 40.1%, followed by Electronics, which generated 8608962.8 or 22.2%.
These sales figures are a testament to the strong performance of the
Food & Beverage category across all Regions.

### Translation

Translate you text using `translate_narrative()` function, specify
Expand All @@ -165,25 +145,6 @@ translation <- translate_narrative(narrative_enhanced, language = "Czech")
cat(translation)
```

Celkové tržby vygenerované ve všech oblastech dosáhly ohromujícího čísla
38790478.4. Po bližším prozkoumání bylo zjištěno, že odlehlé oblasti,
konkrétně NA a EMEA, významně přispěly k tomuto úžasnému číslu. NA
představuje 18079736.4, což představuje 46,6%, zatímco EMEA přispěla
13555412.7, což je 34,9% celkových tržeb.

Další analýza prodejních dat odhalila, že v rámci NA jsou významné
produkty, které přispěly k těmto prodejním číslům, potraviny a nápoje,
které vygenerovaly 7392821 nebo 40,9%, následované elektronikou, která
vygenerovala 3789132.7 nebo 21%. Podobně v rámci EMEA jsou významné
produkty potraviny a nápoje, které vygenerovaly 5265113.2 nebo 38,8%,
následované elektronikou, která vygenerovala 3182803.4 nebo 23,5%.

Je důležité zmínit, že odlehlé produkty, které přispěly k celkovým
tržbám, byly především potraviny a nápoje, které vygenerovaly 15543469.7
nebo 40,1%, následované elektronikou, která vygenerovala 8608962.8 nebo
22,2%. Tyto prodejní čísla jsou důkazem silného výkonu kategorie
potravin a nápojů ve všech oblastech.

### Summarization

If your output is too verbose you can summarize it with
Expand All @@ -194,13 +155,6 @@ summarization <- summarize_narrative(narrative_enhanced)
cat(summarization)
```

Total sales were 38790478.4, with significant contributions from North
America (46.6%) and EMEA (34.9%). Food & Beverage was the top-selling
product in both regions, generating 40.9% and 38.8% of sales,
respectively. Electronics followed with 21% and 23.5% of sales. Food &
Beverage and Electronics were also the top-selling products overall,
contributing 40.1% and 22.2% to total sales, respectively.

# Python

Here are some basic Python examples, for more details visit [pynarrator
Expand Down
7 changes: 0 additions & 7 deletions docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ articles:
formatting: formatting.html
templates: templates.html
trend: trend.html
last_built: 2023-06-25T17:52Z
last_built: 2023-07-03T14:29Z
urls:
reference: https://denisabd.github.io/narrator/reference
article: https://denisabd.github.io/narrator/articles
Expand Down
2 changes: 1 addition & 1 deletion docs/search.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package_prep.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pkgdown::build_site(lazy = FALSE, new_process = TRUE)

# devtools checks and test ---------------------------------------------------------
devtools::test()

devtools::check()

# code coverage
Expand Down

0 comments on commit 1101eed

Please sign in to comment.