-
Notifications
You must be signed in to change notification settings - Fork 218
Closed
Milestone
Description
Aggregation functions only allows input from the column for which the summary value is for. Typical use case shown below where a volume weighted close price uses both close and volume as inputs to the aggregation function.
suppressPackageStartupMessages(library(dplyr))
library(gt)
sp500 %>%
slice_head(n = 22) %>%
select(date, close, volume) %>%
gt() %>%
summary_rows('close', groups = NULL, fns = list('Average close' = ~ mean(.))) %>%
summary_rows('close', groups = NULL, fns = list('Average volume weighted close' = ~ sum(. * volume) / sum(volume)))
#> Error in `summarise()`:
#> ! Problem while computing `close = (function (x) ...`.
#> ℹ The error occurred in group 1: ::group_id:: = "::GRAND_SUMMARY".
#> Caused by error in `fn()`:
#> ! object 'volume' not foundCreated on 2022-05-26 by the reprex package (v2.0.1)
Another example at stackoverflow with ratios
I have cloned the project and changed 'dt_summary.r' to allow access to all columns to get the desired ouput as shown below. I have some missgivings about doing summaries inside gt and then extracting them. I would have prefered to separate data, including summaries, and format completely. Anyways, I can make a pull request for the summary change if desired.
suppressPackageStartupMessages(library(dplyr))
library(gt_aggr)
sp500 %>%
slice_head(n = 22) %>%
select(date, close, volume) %>%
gt() %>%
summary_rows('close', groups = NULL, fns = list('Average close' = ~ mean(.))) %>%
summary_rows('close', groups = NULL, fns = list('Average volume weighted close' = ~ sum(. * volume) / sum(volume)))Created on 2022-05-26 by the reprex package (v2.0.1)
| date | close | volume | |
|---|---|---|---|
| 2015-12-31 | 2043.94 | 2655330000 | |
| 2015-12-30 | 2063.36 | 2367430000 | |
| 2015-12-29 | 2078.36 | 2542000000 | |
| 2015-12-28 | 2056.50 | 2492510000 | |
| 2015-12-24 | 2060.99 | 1411860000 | |
| 2015-12-23 | 2064.29 | 3484090000 | |
| 2015-12-22 | 2038.97 | 3520860000 | |
| 2015-12-21 | 2021.15 | 3760280000 | |
| 2015-12-18 | 2005.55 | 6683070000 | |
| 2015-12-17 | 2041.89 | 4327390000 | |
| 2015-12-16 | 2073.07 | 4635450000 | |
| 2015-12-15 | 2043.41 | 4353540000 | |
| 2015-12-14 | 2021.94 | 4612440000 | |
| 2015-12-11 | 2012.37 | 4301060000 | |
| 2015-12-10 | 2052.23 | 3715150000 | |
| 2015-12-09 | 2047.62 | 4385250000 | |
| 2015-12-08 | 2063.59 | 4173570000 | |
| 2015-12-07 | 2077.07 | 4043820000 | |
| 2015-12-04 | 2091.69 | 4214910000 | |
| 2015-12-03 | 2049.62 | 4306490000 | |
| 2015-12-02 | 2079.51 | 3950640000 | |
| 2015-12-01 | 2102.63 | 3712120000 | |
| Average close | — | 2,054.08 | — |
| Average volume weighted close | — | 2,051.51 | — |
Reactions are currently unavailable