title: How to add a summary column to a gt table that is a function of two other summary columns?
body: <p>I am using gt to show two columns representing values at specific points in time, and a third column that shows a percent change, which was calculated in preprocessing. I would like a summary row that contains the sum of the values in each column, and the percent change between those sums beneath the column of percent changes for the individual observations.</p>
  <p>To illustrate, I modified an example using the sp500 table.</p>
  <pre class="lang-r prettyprint-override"><code>library(gt)
  library(tidyverse)
  
  start_date &lt;- &quot;2010-06-07&quot;
  end_date &lt;- &quot;2010-06-14&quot;
  
  data &lt;- sp500 %&gt;%
    filter(date &gt;= start_date &amp; date &lt;= end_date) %&gt;%
    select(date, open, close) %&gt;%
    mutate(intraday_pct_ch = (close/open - 1)*100)
  
  data
  #&gt; # A tibble: 6 x 4
  #&gt;   date        open close intraday_pct_ch
  #&gt;   &lt;date&gt;     &lt;dbl&gt; &lt;dbl&gt;           &lt;dbl&gt;
  #&gt; 1 2010-06-14 1095  1090.          -0.490
  #&gt; 2 2010-06-11 1083. 1092.           0.827
  #&gt; 3 2010-06-10 1059. 1087.           2.65 
  #&gt; 4 2010-06-09 1063. 1056.          -0.664
  #&gt; 5 2010-06-08 1051. 1062            1.06 
  #&gt; 6 2010-06-07 1066. 1050.          -1.44
  
  data %&gt;% gt() %&gt;%
    summary_rows(
      fns = list(Week_Sum = ~sum(.)),
      columns = vars(open, close, intraday_pct_ch))
  </code></pre>
  <p><sup>Created on 2021-04-20 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v2.0.0)</sup></p>
  <p>As written, I get the sum of the percent changes, which I obviously do not want. Is there a way to write a function here that takes the two previous columns as arguments and would keep the result in the same row?</p>
  <p>Thanks!</p>
  <p><strong>Update:</strong>
  Here is the reprex that gives me the result I want, but it will become unwieldy as the number of &quot;_stock#&quot; groups increases. I deleted the html from the first reprex.</p>
  <pre class="lang-r prettyprint-override"><code>library(gt)
  library(tidyverse)
  
  set.seed(42)  
  data1 &lt;- sp500 %&gt;%
    filter(date &gt;= &quot;2010-06-07&quot; &amp; date &lt;= &quot;2010-06-14&quot;) %&gt;%
    select(date, open, close) %&gt;%
    mutate(intraday_pct_ch = (close/open - 1)*100)
  
  data2 &lt;- data1 %&gt;%
    mutate(open = (open + rnorm(1, 100, 10)),
           close = (close + rnorm(1, 100, 10)),
           intraday_pct_ch = (close/open - 1)*100)
  
  data_bind &lt;- data1 %&gt;% bind_cols(data2, .name_repair = &quot;minimal&quot;) %&gt;%
    rename_with(~ str_replace(., &quot;$&quot;, &quot;_stock1&quot;), .cols = 1:4) %&gt;%
    rename_with(~ str_replace(., &quot;$&quot;, &quot;_stock2&quot;), .cols = 5:8) %&gt;%
    select(!starts_with(&quot;date&quot;))
  
  data_bind
  #&gt; # A tibble: 6 x 6
  #&gt;   open_stock1 close_stock1 intraday_pct_ch_stock1 open_stock2 close_stock2
  #&gt;         &lt;dbl&gt;        &lt;dbl&gt;                  &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;
  #&gt; 1       1095         1090.                 -0.490       1209.        1184.
  #&gt; 2       1083.        1092.                  0.827       1196.        1186.
  #&gt; 3       1059.        1087.                  2.65        1172.        1181.
  #&gt; 4       1063.        1056.                 -0.664       1176.        1150.
  #&gt; 5       1051.        1062                   1.06        1165.        1156.
  #&gt; 6       1066.        1050.                 -1.44        1180.        1145.
  #&gt; # … with 1 more variable: intraday_pct_ch_stock2 &lt;dbl&gt;
  
  #vectorized William Gram's suggested solution
  intraday_pct_ch_vec &lt;- c((sum(data_bind$close_stock1) / sum(data_bind$open_stock1)-1)*100 , (sum(data_bind$close_stock2) / sum(data_bind$open_stock2)-1)*100 )
  
  data_bind %&gt;% gt() %&gt;%
    summary_rows(
      fns = list(Week_Sum = ~sum(.)),
      columns = starts_with(c(&quot;o&quot;, &quot;c&quot;))) %&gt;%
    summary_rows(
      fns = list(Week_Sum = ~ return(intraday_pct_ch_vec[1])),
      columns = 4) %&gt;%
    summary_rows(
      fns = list(Week_Sum = ~ return(intraday_pct_ch_vec[2])),
      columns = 7)
  </code></pre>
  <pre class="lang-html prettyprint-override"><code>
  &lt;style&gt;html {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
  }
  
  #jdrzqoxgdy .gt_table {
    display: table;
    border-collapse: collapse;
    margin-left: auto;
    margin-right: auto;
    color: #333333;
    font-size: 16px;
    font-weight: normal;
    font-style: normal;
    background-color: #FFFFFF;
    width: auto;
    border-top-style: solid;
    border-top-width: 2px;
    border-top-color: #A8A8A8;
    border-right-style: none;
    border-right-width: 2px;
    border-right-color: #D3D3D3;
    border-bottom-style: solid;
    border-bottom-width: 2px;
    border-bottom-color: #A8A8A8;
    border-left-style: none;
    border-left-width: 2px;
    border-left-color: #D3D3D3;
  }
  
  #jdrzqoxgdy .gt_heading {
    background-color: #FFFFFF;
    text-align: center;
    border-bottom-color: #FFFFFF;
    border-left-style: none;
    border-left-width: 1px;
    border-left-color: #D3D3D3;
    border-right-style: none;
    border-right-width: 1px;
    border-right-color: #D3D3D3;
  }
  
  #jdrzqoxgdy .gt_title {
    color: #333333;
    font-size: 125%;
    font-weight: initial;
    padding-top: 4px;
    padding-bottom: 4px;
    border-bottom-color: #FFFFFF;
    border-bottom-width: 0;
  }
  
  #jdrzqoxgdy .gt_subtitle {
    color: #333333;
    font-size: 85%;
    font-weight: initial;
    padding-top: 0;
    padding-bottom: 4px;
    border-top-color: #FFFFFF;
    border-top-width: 0;
  }
  
  #jdrzqoxgdy .gt_bottom_border {
    border-bottom-style: solid;
    border-bottom-width: 2px;
    border-bottom-color: #D3D3D3;
  }
  
  #jdrzqoxgdy .gt_col_headings {
    border-top-style: solid;
    border-top-width: 2px;
    border-top-color: #D3D3D3;
    border-bottom-style: solid;
    border-bottom-width: 2px;
    border-bottom-color: #D3D3D3;
    border-left-style: none;
    border-left-width: 1px;
    border-left-color: #D3D3D3;
    border-right-style: none;
    border-right-width: 1px;
    border-right-color: #D3D3D3;
  }
  
  #jdrzqoxgdy .gt_col_heading {
    color: #333333;
    background-color: #FFFFFF;
    font-size: 100%;
    font-weight: normal;
    text-transform: inherit;
    border-left-style: none;
    border-left-width: 1px;
    border-left-color: #D3D3D3;
    border-right-style: none;
    border-right-width: 1px;
    border-right-color: #D3D3D3;
    vertical-align: bottom;
    padding-top: 5px;
    padding-bottom: 6px;
    padding-left: 5px;
    padding-right: 5px;
    overflow-x: hidden;
  }
  
  #jdrzqoxgdy .gt_column_spanner_outer {
    color: #333333;
    background-color: #FFFFFF;
    font-size: 100%;
    font-weight: normal;
    text-transform: inherit;
    padding-top: 0;
    padding-bottom: 0;
    padding-left: 4px;
    padding-right: 4px;
  }
  
  #jdrzqoxgdy .gt_column_spanner_outer:first-child {
    padding-left: 0;
  }
  
  #jdrzqoxgdy .gt_column_spanner_outer:last-child {
    padding-right: 0;
  }
  
  #jdrzqoxgdy .gt_column_spanner {
    border-bottom-style: solid;
    border-bottom-width: 2px;
    border-bottom-color: #D3D3D3;
    vertical-align: bottom;
    padding-top: 5px;
    padding-bottom: 6px;
    overflow-x: hidden;
    display: inline-block;
    width: 100%;
  }
  
  #jdrzqoxgdy .gt_group_heading {
    padding: 8px;
    color: #333333;
    background-color: #FFFFFF;
    font-size: 100%;
    font-weight: initial;
    text-transform: inherit;
    border-top-style: solid;
    border-top-width: 2px;
    border-top-color: #D3D3D3;
    border-bottom-style: solid;
    border-bottom-width: 2px;
    border-bottom-color: #D3D3D3;
    border-left-style: none;
    border-left-width: 1px;
    border-left-color: #D3D3D3;
    border-right-style: none;
    border-right-width: 1px;
    border-right-color: #D3D3D3;
    vertical-align: middle;
  }
  
  #jdrzqoxgdy .gt_empty_group_heading {
    padding: 0.5px;
    color: #333333;
    background-color: #FFFFFF;
    font-size: 100%;
    font-weight: initial;
    border-top-style: solid;
    border-top-width: 2px;
    border-top-color: #D3D3D3;
    border-bottom-style: solid;
    border-bottom-width: 2px;
    border-bottom-color: #D3D3D3;
    vertical-align: middle;
  }
  
  #jdrzqoxgdy .gt_from_md &gt; :first-child {
    margin-top: 0;
  }
  
  #jdrzqoxgdy .gt_from_md &gt; :last-child {
    margin-bottom: 0;
  }
  
  #jdrzqoxgdy .gt_row {
    padding-top: 8px;
    padding-bottom: 8px;
    padding-left: 5px;
    padding-right: 5px;
    margin: 10px;
    border-top-style: solid;
    border-top-width: 1px;
    border-top-color: #D3D3D3;
    border-left-style: none;
    border-left-width: 1px;
    border-left-color: #D3D3D3;
    border-right-style: none;
    border-right-width: 1px;
    border-right-color: #D3D3D3;
    vertical-align: middle;
    overflow-x: hidden;
  }
  
  #jdrzqoxgdy .gt_stub {
    color: #333333;
    background-color: #FFFFFF;
    font-size: 100%;
    font-weight: initial;
    text-transform: inherit;
    border-right-style: solid;
    border-right-width: 2px;
    border-right-color: #D3D3D3;
    padding-left: 12px;
  }
  
  #jdrzqoxgdy .gt_summary_row {
    color: #333333;
    background-color: #FFFFFF;
    text-transform: inherit;
    padding-top: 8px;
    padding-bottom: 8px;
    padding-left: 5px;
    padding-right: 5px;
  }
  
  #jdrzqoxgdy .gt_first_summary_row {
    padding-top: 8px;
    padding-bottom: 8px;
    padding-left: 5px;
    padding-right: 5px;
    border-top-style: solid;
    border-top-width: 2px;
    border-top-color: #D3D3D3;
  }
  
  #jdrzqoxgdy .gt_grand_summary_row {
    color: #333333;
    background-color: #FFFFFF;
    text-transform: inherit;
    padding-top: 8px;
    padding-bottom: 8px;
    padding-left: 5px;
    padding-right: 5px;
  }
  
  #jdrzqoxgdy .gt_first_grand_summary_row {
    padding-top: 8px;
    padding-bottom: 8px;
    padding-left: 5px;
    padding-right: 5px;
    border-top-style: double;
    border-top-width: 6px;
    border-top-color: #D3D3D3;
  }
  
  #jdrzqoxgdy .gt_striped {
    background-color: rgba(128, 128, 128, 0.05);
  }
  
  #jdrzqoxgdy .gt_table_body {
    border-top-style: solid;
    border-top-width: 2px;
    border-top-color: #D3D3D3;
    border-bottom-style: solid;
    border-bottom-width: 2px;
    border-bottom-color: #D3D3D3;
  }
  
  #jdrzqoxgdy .gt_footnotes {
    color: #333333;
    background-color: #FFFFFF;
    border-bottom-style: none;
    border-bottom-width: 2px;
    border-bottom-color: #D3D3D3;
    border-left-style: none;
    border-left-width: 2px;
    border-left-color: #D3D3D3;
    border-right-style: none;
    border-right-width: 2px;
    border-right-color: #D3D3D3;
  }
  
  #jdrzqoxgdy .gt_footnote {
    margin: 0px;
    font-size: 90%;
    padding: 4px;
  }
  
  #jdrzqoxgdy .gt_sourcenotes {
    color: #333333;
    background-color: #FFFFFF;
    border-bottom-style: none;
    border-bottom-width: 2px;
    border-bottom-color: #D3D3D3;
    border-left-style: none;
    border-left-width: 2px;
    border-left-color: #D3D3D3;
    border-right-style: none;
    border-right-width: 2px;
    border-right-color: #D3D3D3;
  }
  
  #jdrzqoxgdy .gt_sourcenote {
    font-size: 90%;
    padding: 4px;
  }
  
  #jdrzqoxgdy .gt_left {
    text-align: left;
  }
  
  #jdrzqoxgdy .gt_center {
    text-align: center;
  }
  
  #jdrzqoxgdy .gt_right {
    text-align: right;
    font-variant-numeric: tabular-nums;
  }
  
  #jdrzqoxgdy .gt_font_normal {
    font-weight: normal;
  }
  
  #jdrzqoxgdy .gt_font_bold {
    font-weight: bold;
  }
  
  #jdrzqoxgdy .gt_font_italic {
    font-style: italic;
  }
  
  #jdrzqoxgdy .gt_super {
    font-size: 65%;
  }
  
  #jdrzqoxgdy .gt_footnote_marks {
    font-style: italic;
    font-size: 65%;
  }
  &lt;/style&gt;
  &lt;div id=&quot;jdrzqoxgdy&quot; style=&quot;overflow-x:auto;overflow-y:auto;width:auto;height:auto;&quot;&gt;&lt;table class=&quot;gt_table&quot;&gt;
    
    &lt;thead class=&quot;gt_col_headings&quot;&gt;
      &lt;tr&gt;
        &lt;th class=&quot;gt_col_heading gt_columns_bottom_border gt_left&quot; rowspan=&quot;1&quot; colspan=&quot;1&quot;&gt;&lt;/th&gt;
        &lt;th class=&quot;gt_col_heading gt_columns_bottom_border gt_right&quot; rowspan=&quot;1&quot; colspan=&quot;1&quot;&gt;open_stock1&lt;/th&gt;
        &lt;th class=&quot;gt_col_heading gt_columns_bottom_border gt_right&quot; rowspan=&quot;1&quot; colspan=&quot;1&quot;&gt;close_stock1&lt;/th&gt;
        &lt;th class=&quot;gt_col_heading gt_columns_bottom_border gt_right&quot; rowspan=&quot;1&quot; colspan=&quot;1&quot;&gt;intraday_pct_ch_stock1&lt;/th&gt;
        &lt;th class=&quot;gt_col_heading gt_columns_bottom_border gt_right&quot; rowspan=&quot;1&quot; colspan=&quot;1&quot;&gt;open_stock2&lt;/th&gt;
        &lt;th class=&quot;gt_col_heading gt_columns_bottom_border gt_right&quot; rowspan=&quot;1&quot; colspan=&quot;1&quot;&gt;close_stock2&lt;/th&gt;
        &lt;th class=&quot;gt_col_heading gt_columns_bottom_border gt_right&quot; rowspan=&quot;1&quot; colspan=&quot;1&quot;&gt;intraday_pct_ch_stock2&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody class=&quot;gt_table_body&quot;&gt;
      &lt;tr&gt;
        &lt;td class=&quot;gt_row gt_left gt_stub&quot;&gt;&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1095.00&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1089.63&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;-0.4904110&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1208.71&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1183.983&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;-2.0456995&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;gt_row gt_left gt_stub&quot;&gt;&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1082.65&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1091.60&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;0.8266753&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1196.36&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1185.953&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;-0.8698527&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;gt_row gt_left gt_stub&quot;&gt;&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1058.77&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1086.84&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;2.6511896&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1172.48&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1181.193&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;0.7431629&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;gt_row gt_left gt_stub&quot;&gt;&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1062.75&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1055.69&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;-0.6643237&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1176.46&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1150.043&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;-2.2454376&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;gt_row gt_left gt_stub&quot;&gt;&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1050.81&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1062.00&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1.0648832&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1164.52&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1156.353&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;-0.7012905&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;gt_row gt_left gt_stub&quot;&gt;&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1065.84&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1050.47&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;-1.4420551&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1179.55&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;1144.823&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right&quot;&gt;-2.9440531&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;gt_row gt_stub gt_right gt_grand_summary_row gt_first_grand_summary_row&quot;&gt;Week_Sum&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row&quot;&gt;6,415.82&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row&quot;&gt;6,436.23&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row&quot;&gt;0.32&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row&quot;&gt;7,098.08&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row&quot;&gt;7,002.35&lt;/td&gt;
        &lt;td class=&quot;gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row&quot;&gt;&amp;minus;1.35&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
    
    
  &lt;/table&gt;&lt;/div&gt;
  
  &lt;sup&gt;Created on 2021-04-21 by the [reprex package](https://reprex.tidyverse.org) (v2.0.0)&lt;/sup&gt;
  </code></pre>