@@ -42,6 +42,64 @@ test_that("gt_group() can be used to contain gt tables", {
4242 expect_s3_class(gt_tbls_1 [[" gt_tbl_options" ]], " tbl_df" )
4343})
4444
45+ test_that(" gt_group() can be used to contain gt tables and existing gt_groups" , {
46+
47+ # Create two different `gt_tbl` table objects
48+ gt_tbl_1 <- gt(exibble )
49+ gt_tbl_2 <- gt(gtcars )
50+
51+ # Create a `gt_group` object with `gt_group()`
52+ gt_grp_1 <- gt_group(gt_tbl_1 , gt_tbl_2 )
53+
54+ # Create a new gt group from a list of all existing tables - including a gt_group
55+ gt_list <- list (gt_tbl_1 , gt_tbl_2 , gt_grp_1 )
56+
57+ gt_tbls_1 <- gt_group(.list = gt_list )
58+
59+ # Expect that the `gt_tbls_1` object produced by `gt_group()`
60+ # has the 'gt_group' class
61+ expect_s3_class(gt_tbls_1 , " gt_group" )
62+ expect_type(gt_tbls_1 , " list" )
63+
64+ # The gt_group should contain 4 tables:
65+ # 2 individual tables + 1 group of 2 tables (flattened)
66+ expect_equal(nrow(gt_tbls_1 [[" gt_tbls" ]]), 4L )
67+
68+ # Verify that the flattened tables match the originals
69+ expect_equal(grp_pull(gt_tbls_1 , 1 ), gt_tbl_1 )
70+ expect_equal(grp_pull(gt_tbls_1 , 2 ), gt_tbl_2 )
71+ expect_equal(grp_pull(gt_tbls_1 , 3 ), grp_pull(gt_grp_1 , 1 ))
72+ expect_equal(grp_pull(gt_tbls_1 , 4 ), grp_pull(gt_grp_1 , 2 ))
73+
74+ # Expect that `gt_group()` also works when passing gt_group objects via `...`
75+ gt_tbls_2 <- gt_group(gt_tbl_1 , gt_grp_1 )
76+ expect_s3_class(gt_tbls_2 , " gt_group" )
77+ expect_equal(nrow(gt_tbls_2 [[" gt_tbls" ]]), 3L )
78+
79+ # Setting the option `.use_grp_opts` means that the internal
80+ # component of similar naming is set to that logical value
81+ # Create a `gt_group` object with `gt_group()`
82+ gt_tbls_3 <- gt_group(.list = gt_list , .use_grp_opts = TRUE )
83+ expect_true(gt_tbls_3 [[" use_grp_opts" ]])
84+ gt_tbls_4 <- gt_group(.list = gt_list , .use_grp_opts = FALSE )
85+ expect_false(gt_tbls_4 [[" use_grp_opts" ]])
86+
87+ # Not setting it means it will be FALSE by default
88+ gt_tbls_5 <- gt_group(.list = gt_list )
89+ expect_false(gt_tbls_5 [[" use_grp_opts" ]])
90+
91+ # Expect specific components inside of a 'gt_group' object
92+ expect_named(
93+ gt_tbls_1 ,
94+ c(" gt_tbls" , " gt_tbl_options" , " use_grp_opts" )
95+ )
96+
97+ # Expect that the 'gt_tbls' and `gt_tbl_options` objects inside of
98+ # 'gt_group' are both tibbles
99+ expect_s3_class(gt_tbls_1 [[" gt_tbls" ]], " tbl_df" )
100+ expect_s3_class(gt_tbls_1 [[" gt_tbl_options" ]], " tbl_df" )
101+ })
102+
45103test_that(" grp_pull() can be used to extract a table from a group" , {
46104
47105 # Create two different `gt_tbl` table objects
0 commit comments