0
votes

I want to report data in R using the display format like an MS Access block grouped report, that is, where the data that is duplicated on the subsequent lines of a group is omitted.

library(gt)
library(tidyverse)


df = tribble( ~a, ~b, ~c,
             'a', 'b', 'c',
             'a', 'b', 'd',
             'a', 'b', 'e',
             '1', 'b', 'c',
             '1', 'b', 'd',
             '1', 'b', 'e')


Desired output here (if you can imagine the red to be cut out)

enter image description here

I tried 2 different things. The plain gt() call comes close, but I want to get rid of the duplicated values, e.g. the values a/b in rows 2-3 and 1/b in rows 5-6.s


df %>% gt()

plain gt() output

First, I grouped by 2 columns, but then I lost my column level formatting, and descriptive headers.


df %>%
  group_by( a, b) %>%
  gt()

grouped gt output

2

2 Answers

0
votes

Maybe you can turn the duplicated values to blank -

df[duplicated(df[1:2]), 1:2] <- ''
gt::gt(df)

enter image description here

0
votes

We can use replace

library(dplyr)
library(gt)
df %>%
    mutate(across(1:2, ~ replace(., duplicated(.), ""))) %>%
    gt()