Skip to content

Add ability to do formula based on other columns in Summary Rows #383

@jestarr

Description

@jestarr

Here's an easy example. I have a gt table where I get the sum of a variety of columns. One of the columns is for % of value. I'd like to get the % of value based on the sums in the Summary/Grand Summary Row as well.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <-
  tibble::tribble(
    ~price, ~value,
    5, 4,
    2, 6,
    3, 1
  )


df <-
  df %>%
  mutate(pct_of_value = value / price)

df
#> # A tibble: 3 x 3
#>   price value pct_of_value
#>   <dbl> <dbl>        <dbl>
#> 1     5     4        0.8  
#> 2     2     6        3    
#> 3     3     1        0.333

summary_row <-
  df %>%
  summarise_at(vars(price, value), sum) %>%
  mutate(pct_of_value = value / price)

summary_row
#> # A tibble: 1 x 3
#>   price value pct_of_value
#>   <dbl> <dbl>        <dbl>
#> 1    10    11          1.1
Created on 2019-10-24 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions