# NOT RUN {
# Use `sp500` to create a gt table with
# row groups; create summary rows by row
# group (`min`, `max`, `avg`) and then
# extract the summary rows as a list
# object
summary_extracted <-
sp500 %>%
dplyr::filter(
date >= "2015-01-05" &
date <="2015-01-30"
) %>%
dplyr::arrange(date) %>%
dplyr::mutate(
week = paste0(
"W", strftime(date, format = "%V"))
) %>%
dplyr::select(-adj_close, -volume) %>%
gt(
rowname_col = "date",
groupname_col = "week"
) %>%
summary_rows(
groups = TRUE,
columns = vars(open, high, low, close),
fns = list(
min = ~min(.),
max = ~max(.),
avg = ~mean(.)),
formatter = fmt_number,
use_seps = FALSE
) %>%
extract_summary()
# Use the summary list to make a new
# gt table; the key thing is to use
# `dplyr::bind_rows()` and then pass the
# tibble to `gt()` (the `groupname` and
# `rowname` magic column names create
# row groups and a stub)
tab_1 <-
summary_extracted %>%
unlist(recursive = FALSE) %>%
dplyr::bind_rows() %>%
gt()
# }
Run the code above in your browser using DataLab