Selecting different row-column combinations in tab_footnote locations for gt package in R

  gt, r, windows

Is there a way to select different rows per column for the same footnote in the gt package in R?

df = data.frame(Indicators = c("P1","P2","P3","P4","P5"), 
                Grp1 = c(3,2,1,1,4), 
                Grp2 = c(3,4,4,2,2), 
                Grp3 = c(1,1,2,1,4))

  Indicators Grp1 Grp2 Grp3
1         P1    3    3    1
2         P2    2    4    1
3         P3    1    4    2
4         P4    1    2    1
5         P5    4    2    4

I have a reference table for the cells (column names and row numbers) where the footnote should appear.

lookup <- data.frame(grp = c("Grp2","Grp2","Grp3"),
                     rows = c(2,4,1))

   grp rows
1 Grp2    2
2 Grp2    4
3 Grp3    1

How can I use the lookup table in the locations argument for tab_footnote properly?

The code below returns a table with a footnote for the same rows for Grp2 and Grp3 instead of only rows 2 and 4 for Grp2 and row 1 for Grp3.

df %>% 
    gt() %>% 
    tab_footnote(footnote = "Corrected value", 
                 locations = cells_body(columns = lookup$grp, rows = lookup$rows))

Source: Windows Questions

LEAVE A COMMENT