-
Notifications
You must be signed in to change notification settings - Fork 197
/
test-gt_object.R
794 lines (687 loc) · 23.3 KB
/
test-gt_object.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
context("Ensuring that the `gt()` function works as expected")
# Function to skip tests if Suggested packages not available on system
check_suggests <- function() {
skip_if_not_installed("rvest")
skip_if_not_installed("xml2")
}
# Gets the inner HTML text from a single value
selection_text <- function(html, selection) {
html %>%
rvest::html_nodes(selection) %>%
rvest::html_text()
}
test_that("a gt table object contains the correct components", {
# Create a `gt_tbl` object with `gt()`
tab <- iris %>% gt()
# Expect that the `gt_tbl` object has all of the
# usual components and that they have all of the
# expected dimensions and features
expect_tab(tab = tab, df = iris)
# Expect that the `stub_df` data frame is correctly
# formed given the input rownames and groupnames
expect_tab_colnames(
tab = tab,
df = iris,
rowname = "NA",
groupname_is_na = TRUE
)
# Create a `gt_tbl` object with `gt()` and a
# grouped version of the `iris` dataset
tab <-
iris %>% dplyr::group_by(Species) %>%
gt()
# Expect that the `gt_tbl` object has all of the
# usual components and that they have all of the
# expected dimensions and features
#expect_tab(tab, df = iris %>% dplyr::group_by(Species))
})
test_that("a gt table can be made to use the rownames of a data frame", {
# Create a `gt_tbl` object with `gt()` and use the
# data frame's row names as row names in the stub
tab <-
mtcars %>%
gt(rownames_to_stub = TRUE)
# Expect that the `gt_tbl` object has all of the
# usual components and that they have all of the
# expected dimensions and features
expect_tab(tab = tab, df = mtcars)
# Expect that the `stub_df` data frame is correctly
# formed given the input rownames and groupnames
expect_tab_colnames(
tab,
df = mtcars,
rowname = "tibble",
groupname_is_na = TRUE
)
})
test_that("a gt table can be made with the stub partially or fully populated", {
# Create an input data frame with a `rowname` column
# and a `value` column
data_r <-
data.frame(
rowname = letters[1:10],
value = 1:10,
stringsAsFactors = FALSE
)
# Create a `gt_tbl` object with `gt()` and the
# `data_r` dataset
tab <- data_r %>% gt()
# Expect that the `gt_tbl` object has all of the
# usual components and that they have all of the
# expected dimensions and features
expect_tab(tab = tab, df = data_r)
# Expect that the `stub_df` data frame is correctly
# formed given the input rownames and groupnames
expect_tab_colnames(
tab, df = data_r,
rowname = "col",
groupname_is_na = TRUE
)
# Create an input data frame with a `rowname` column,
# a `groupname` column, and a `value` column
data_rg <-
data.frame(
rowname = letters[1:10],
groups = c("A", "A", "A", "B", "B", "B", "C", "C", "D", "D"),
value = 1:10,
stringsAsFactors = FALSE
)
# Create a `gt_tbl` object with `gt()` and the
# `data_rg` dataset
tab <- data_rg %>% gt(groupname_col = "groups")
# Expect that the `gt_tbl` object has all of the
# usual components and that they have all of the
# expected dimensions and features
expect_tab(tab = tab, df = data_rg)
# Expect that the `stub_df` data frame is correctly
# formed given the input rownames and groupnames
expect_tab_colnames(
tab = tab,
df = data_rg,
rowname = "col",
groupname_is_na = FALSE
)
})
test_that("a gt table can be made from a table with no rows", {
# Create an input data frame based on the exibble
# dataset, except with no rows
data_e <- exibble %>% head(0)
# Create a `gt_tbl` object with `gt()` and the
# `data_e` dataset
tab <- data_e %>% gt()
# Expect that the `gt_tbl` object has all of the
# usual components and that they have all of the
# expected dimensions and features
expect_tab(tab = tab, df = data_e)
# Expect that the `stub_df` data frame is empty
dt_stub_df_get(data = tab) %>%
nrow() %>%
expect_equal(0)
# Create a `gt_tbl` object with `gt()` and a
# grouped version of the `data_e` dataset
tab <-
data_e %>%
dplyr::group_by(group) %>%
gt()
# Expect that the `gt_tbl` object has all of the
# usual components and that they have all of the
# expected dimensions and features
expect_tab(tab = tab, df = data_e %>% dplyr::group_by(group))
})
test_that("a gt table can use UTF-8 chars in any system locale", {
# Check that specific suggested packages are available
check_suggests()
# Skip these tests if the platform is Windows or Solaris
skip_on_os(os = c("windows", "solaris"))
system_locales <-
c("nl_NL.UTF-8", "pt_BR.UTF-8", "cs_CZ.UTF-8", "zh_CN.UTF-8",
"en_US.UTF-8", "sk_SK.UTF-8", "de_DE.UTF-8", "am_ET.UTF-8", "be_BY.UTF-8",
"af_ZA.UTF-8", "ro_RO.UTF-8", "en_NZ.UTF-8", "fi_FI.UTF-8", "hr_HR.UTF-8",
"da_DK.UTF-8", "de_AT.UTF-8", "en_AU.UTF-8", "hu_HU.UTF-8", "et_EE.UTF-8",
"he_IL.UTF-8", "fr_BE.UTF-8", "fr_CH.UTF-8", "sl_SI.UTF-8", "pl_PL.UTF-8",
"sv_SE.UTF-8", "sr_YU.UTF-8", "de_CH.UTF-8", "pt_PT.UTF-8", "ca_ES.UTF-8",
"en_GB.UTF-8", "ru_RU.UTF-8", "eu_ES.UTF-8", "it_CH.UTF-8", "nl_BE.UTF-8",
"lt_LT.UTF-8", "is_IS.UTF-8", "zh_TW.UTF-8", "el_GR.UTF-8", "it_IT.UTF-8",
"en_CA.UTF-8", "uk_UA.UTF-8", "zh_HK.UTF-8", "bg_BG.UTF-8", "ja_JP.UTF-8",
"fr_FR.UTF-8", "ko_KR.UTF-8", "kk_KZ.UTF-8", "en_IE.UTF-8", "fr_CA.UTF-8",
"hy_AM.UTF-8", "no_NO.UTF-8", "es_ES.UTF-8", "tr_TR.UTF-8",
"en_NZ", "af_ZA", "bg_BG", "fi_FI", "eu_ES", "nl_BE", "fr_BE", "sk_SK",
"de_CH", "zh_HK", "uk_UA", "en_US", "am_ET", "sv_SE", "be_BY", "kk_KZ",
"it_CH", "pt_BR", "ko_KR", "it_IT", "zh_TW", "en_CA", "pt_PT", "hr_HR",
"cs_CZ", "fr_CH", "he_IL", "fr_CA", "sl_SI", "ro_RO", "ja_JP", "zh_CN",
"nl_NL", "hy_AM", "hu_HU", "en_AU", "en_GB", "et_EE", "ca_ES", "sr_YU",
"ru_RU", "is_IS", "es_ES", "el_GR", "da_DK", "no_NO", "en_IE", "pl_PL",
"fr_FR", "de_AT", "de_DE", "lt_LT", "tr_TR",
"C", "POSIX"
)
# Intersect the vector of available locales (on a given test system)
# with the `system_locales` vector
available_locales <- system("locale -a", intern = TRUE)
system_locales <- intersect(system_locales, available_locales)
names_df <-
data.frame(
first = c("\u6625", "Fred"),
last = c("\u9ad8","B\u00e4ckhed"),
stringsAsFactors = FALSE
)
# Function to check for identical vectors in a df processed by
# gt and an expectation vector
test_df_output_with_sys_locale <- function(df, locale, expected_vec) {
old_ctype <- Sys.getlocale("LC_CTYPE")
Sys.setlocale("LC_CTYPE", locale = locale)
on.exit(Sys.setlocale("LC_CTYPE", old_ctype), add = TRUE)
# Expect that UTF-8 characters are retained in the
# gt object after rendering
expect_identical(
expected_vec %>% sort(),
(df %>% gt() %>% render_formats_test("html")) %>%
unlist() %>% unname() %>% sort()
)
# Expect that UTF-8 characters are retained in the
# rendered HTML table
expect_identical(
expected_vec %>% sort(),
df %>%
gt() %>%
tab_options(row.striping.include_table_body = FALSE) %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_row gt_left']") %>%
sort()
)
}
# Run tests with `test_df_output_with_sys_locale()` for all locales
# in the `system_locales` vector
for (locale in system_locales) {
test_df_output_with_sys_locale(
df = names_df,
locale = locale,
expected_vec = c("B\u00e4ckhed", "Fred", "\u6625", "\u9ad8")
)
}
})
test_that("gt table can be made with grouped data -- one group", {
# Use `dplyr::group_by()` to add a group to an
# incoming table; create the gt object and build
# its data under an `html` context
built_tbl <-
exibble %>%
dplyr::group_by(group) %>%
gt() %>%
build_data(context = "html")
# Expect that groups are available in the gt object
built_tbl$`_row_groups` %>% expect_equal(c("grp_a", "grp_b"))
built_tbl$`_groups_rows` %>%
expect_equivalent(
data.frame(
group_id = c("grp_a", "grp_b"),
group_label = c("grp_a", "grp_b"),
row_start = c(1, 5),
row_end = c(4, 8),
stringsAsFactors = FALSE
)
)
built_tbl$`_boxhead` %>% .[, 1:2] %>%
expect_equal(
dplyr::tibble(
var = colnames(exibble),
type = c(rep("default", 8), "row_group")
)
)
built_tbl$`_stub_df` %>%
expect_equivalent(
dplyr::tibble(
rownum_i = 1:8,
group_id = c(rep("grp_a", 4), rep("grp_b", 4)),
rowname = NA_character_,
group_label = c(rep("grp_a", 4), rep("grp_b", 4)),
built = c(rep("grp_a", 4), rep("grp_b", 4))
)
)
# Render the HTML table and read the HTML with `xml2`
html_tbl <-
exibble %>%
dplyr::group_by(group) %>%
gt(auto_align = FALSE) %>%
render_as_html() %>%
xml2::read_html()
# Expect 8 column labels in the rendered HTML table
html_tbl %>%
rvest::html_nodes("[class='gt_col_heading gt_columns_bottom_border gt_center']") %>%
rvest::html_text() %>%
expect_equal(colnames(exibble) %>% setdiff("group"))
# Expect two labels in group heading rows (above each row group)
html_tbl %>%
rvest::html_nodes("[class='gt_group_heading']") %>%
rvest::html_text() %>%
expect_equal(c("grp_a", "grp_b"))
# Expect the first row of data to match that which is expected
html_tbl %>%
rvest::html_nodes("[class='gt_row gt_center']") %>%
rvest::html_text() %>%
.[1:8] %>%
expect_equal(
c(
"1.111e-01", "apricot", "one", "2015-01-15", "13:35",
"2018-01-01 02:22", "49.950", "row_1"
)
)
})
test_that("gt table can be made with grouped data - two groups", {
# Use `dplyr::group_by()` to add two groups to an
# incoming table; create the gt object and build
# its data under an `html` context
built_tbl <-
exibble %>%
dplyr::mutate(group_2 = rep(paste0("grp_", 1:4), 2) %>% sort()) %>%
dplyr::group_by(group, group_2) %>%
gt() %>%
build_data(context = "html")
table_groups <-
c("grp_a - grp_1", "grp_a - grp_2", "grp_b - grp_3", "grp_b - grp_4")
# Expect that groups are available in the gt object
built_tbl$`_row_groups` %>%
expect_equal(table_groups)
built_tbl$`_groups_rows` %>%
expect_equivalent(
dplyr::tibble(
group_id = table_groups,
group_label = table_groups,
row_start = c(1, 3, 5, 7),
row_end = c(2, 4, 6, 8)
)
)
built_tbl$`_boxhead` %>% .[, 1:2] %>%
expect_equal(
dplyr::tibble(
var = colnames(
exibble %>%
dplyr::mutate(group_2 = rep(paste0("grp_", 1:4), 2) %>% sort())
),
type = c(rep("default", 8), rep("row_group", 2))
)
)
built_tbl$`_stub_df` %>%
expect_equivalent(
dplyr::tibble(
rownum_i = 1:8,
group_id = rep(table_groups, 2) %>% sort(),
rowname = NA_character_,
group_label = rep(table_groups, 2) %>% sort(),
built = rep(table_groups, 2) %>% sort()
)
)
# Render the HTML table and read the HTML with `xml2`
html_tbl <-
exibble %>%
dplyr::mutate(group_2 = rep(paste0("grp_", 1:4), 2) %>% sort()) %>%
dplyr::group_by(group, group_2) %>%
gt(auto_align = FALSE) %>%
render_as_html() %>%
xml2::read_html()
# Expect 8 column labels in the rendered HTML table
html_tbl %>%
rvest::html_nodes("[class='gt_col_heading gt_columns_bottom_border gt_center']") %>%
rvest::html_text() %>%
expect_equal(colnames(exibble) %>% setdiff("group"))
# Expect four labels in group heading rows (above each row group)
html_tbl %>%
rvest::html_nodes("[class='gt_group_heading']") %>%
rvest::html_text() %>%
expect_equal(
c(
"grp_a - grp_1", "grp_a - grp_2", "grp_b - grp_3", "grp_b - grp_4"
)
)
# Expect the first row of data to match that which is expected
html_tbl %>%
rvest::html_nodes("[class='gt_row gt_center']") %>%
rvest::html_text() %>%
.[1:8] %>%
expect_equal(
c(
"1.111e-01", "apricot", "one", "2015-01-15",
"13:35", "2018-01-01 02:22", "49.950", "row_1"
)
)
})
test_that("The `gt()` groupname_col arg will override any grouped data", {
# Use `dplyr::group_by()` to add a group to an
# incoming table; create the gt object and provide
# a value for the `groupname_col` arg (which we
# expect will be used over tbl groups)
built_tbl <-
exibble %>%
dplyr::group_by(group) %>%
gt(groupname_col = "date") %>%
build_data(context = "html")
# Expect that groups are available in the gt object
built_tbl$`_row_groups` %>%
expect_equal(
c(
"2015-01-15", "2015-02-15", "2015-03-15", "2015-04-15",
"2015-05-15", "2015-06-15", "NA", "2015-08-15"
)
)
built_tbl$`_groups_rows` %>%
expect_equivalent(
data.frame(
group_id = c(
"2015-01-15", "2015-02-15", "2015-03-15", "2015-04-15",
"2015-05-15", "2015-06-15", "NA", "2015-08-15"),
group_label = c(
"2015-01-15", "2015-02-15", "2015-03-15", "2015-04-15",
"2015-05-15", "2015-06-15", "NA", "2015-08-15"),
row_start = 1:8,
row_end = 1:8,
stringsAsFactors = FALSE
)
)
built_tbl$`_boxhead` %>% .[, 1:2] %>%
expect_equal(
dplyr::tibble(
var = colnames(exibble),
type = c(rep("default", 3), "row_group", rep("default", 5))
)
)
built_tbl$`_stub_df` %>%
expect_equivalent(
dplyr::tibble(
rownum_i = 1:8,
group_id = c(
"2015-01-15", "2015-02-15", "2015-03-15", "2015-04-15",
"2015-05-15", "2015-06-15", "NA", "2015-08-15"
),
rowname = NA_character_,
group_label = c(
"2015-01-15", "2015-02-15", "2015-03-15", "2015-04-15",
"2015-05-15", "2015-06-15", "NA", "2015-08-15"
),
built = c(
"2015-01-15", "2015-02-15", "2015-03-15", "2015-04-15",
"2015-05-15", "2015-06-15", "NA", "2015-08-15"
)
)
)
# Render the HTML table and read the HTML with `xml2`
html_tbl <-
exibble %>%
dplyr::group_by(group) %>%
gt(auto_align = FALSE) %>%
render_as_html() %>%
xml2::read_html()
# Expect 8 column labels in the rendered HTML table
html_tbl %>%
rvest::html_nodes("[class='gt_col_heading gt_columns_bottom_border gt_center']") %>%
rvest::html_text() %>%
expect_equal(colnames(exibble) %>% setdiff("group"))
# Expect two labels in group heading rows (above each row group)
html_tbl %>%
rvest::html_nodes("[class='gt_group_heading']") %>%
rvest::html_text() %>%
expect_equal(c("grp_a", "grp_b"))
# Expect the first row of data to match that which is expected
html_tbl %>%
rvest::html_nodes("[class='gt_row gt_center']") %>%
rvest::html_text() %>%
.[1:8] %>%
expect_equal(
c(
"1.111e-01", "apricot", "one", "2015-01-15", "13:35",
"2018-01-01 02:22", "49.950", "row_1"
)
)
})
test_that("The `gt()` `id` arg will only accept specific inputs", {
# Expect no errors with valid inputs to `id`
expect_error(regexp = NA, exibble %>% gt())
expect_error(regexp = NA, exibble %>% gt(id = NULL))
expect_error(regexp = NA, exibble %>% gt(id = "sldfjlds"))
expect_error(regexp = NA, exibble %>% gt(id = "23947294"))
# Expect errors when `id` isn't a length 1 character vector or is NA
expect_error(exibble %>% gt(id = 23))
expect_error(exibble %>% gt(id = c("one", "two")))
expect_error(exibble %>% gt(id = NA))
})
test_that("The `gt()` `rowname_col` arg will be overridden by `rownames_to_stub = TRUE`", {
# Create a gt object where rownames will be used as
# stub labels, and, provide a value for the `rowname_col`
# arg (which we expect will be ignored in favor of the
# table's row names)
built_tbl <-
mtcars[1:10, ] %>%
gt(rownames_to_stub = TRUE, rowname_col = "disp") %>%
build_data(context = "html")
# Expect that no groups are available in the gt object
built_tbl$`_row_groups` %>% expect_equal(character(0))
# Expect no rows in `_groups_rows`
built_tbl$`_groups_rows` %>%
nrow() %>%
expect_equal(0)
built_tbl$`_boxhead` %>% .[, 1:2] %>%
expect_equal(
dplyr::tibble(
var = c("__GT_ROWNAME_PRIVATE__", colnames(mtcars)),
type = c("stub", rep("default", 11))
)
)
built_tbl$`_stub_df` %>%
expect_equivalent(
dplyr::tibble(
rownum_i = 1:10,
group_id = NA_character_,
rowname = rownames(mtcars)[1:10],
group_label = list(NULL),
built = ""
)
)
# Render the HTML table and read the HTML with `xml2`
html_tbl <-
mtcars[1:10, ] %>%
gt(rownames_to_stub = TRUE, rowname_col = "disp", auto_align = FALSE) %>%
render_as_html() %>%
xml2::read_html()
# Expect 11 column labels in the rendered HTML table
html_tbl %>%
rvest::html_nodes("[class='gt_col_heading gt_columns_bottom_border gt_center']") %>%
rvest::html_text() %>%
expect_equal(colnames(mtcars))
# Expect no labels in group heading rows (above each row group)
html_tbl %>%
rvest::html_nodes("[class='gt_group_heading']") %>%
rvest::html_text() %>%
expect_equal(character(0))
# Expect the first row of data to match that which is expected
html_tbl %>%
rvest::html_nodes("[class='gt_row gt_center']") %>%
rvest::html_text() %>%
.[1:11] %>%
expect_equal(
c(
"21.0", "6", "160.0", "110", "3.90",
"2.620", "16.46", "0", "1", "4", "4"
)
)
})
test_that("The `rowname` column will be safely included when `rownames_to_stub = TRUE`", {
# Create a gt object where rownames will be used as
# stub labels, provide a column named `rowname`, and
# use `rownames_to_stub = FALSE`
built_tbl <-
exibble %>%
dplyr::mutate(rowname = "test") %>%
gt(rownames_to_stub = TRUE) %>%
build_data(context = "html")
# Expect that no groups are available in the gt object
built_tbl$`_row_groups` %>% expect_equal(character(0))
# Expect no rows in `_groups_rows`
built_tbl$`_groups_rows` %>%
nrow() %>%
expect_equal(0)
built_tbl$`_boxhead` %>% .[, 1:2] %>%
expect_equal(
dplyr::tibble(
var = c("__GT_ROWNAME_PRIVATE__", colnames(exibble), "rowname"),
type = c("stub", rep("default", 10))
)
)
built_tbl$`_stub_df` %>%
expect_equivalent(
dplyr::tibble(
rownum_i = 1:8,
group_id = NA_character_,
rowname = as.character(1:8),
group_label = list(NULL),
built = ""
)
)
# Render the HTML table and read the HTML with `xml2`
html_tbl <-
exibble %>%
dplyr::mutate(rowname = "test") %>%
gt(rownames_to_stub = TRUE, auto_align = FALSE) %>%
render_as_html() %>%
xml2::read_html()
# Expect 10 column labels in the rendered HTML table
html_tbl %>%
rvest::html_nodes("[class='gt_col_heading gt_columns_bottom_border gt_center']") %>%
rvest::html_text() %>%
expect_equal(c(colnames(exibble), "rowname"))
# Expect no labels in group heading rows (above each row group)
html_tbl %>%
rvest::html_nodes("[class='gt_group_heading']") %>%
rvest::html_text() %>%
expect_equal(character(0))
# Expect the first row of data to match that which is expected
html_tbl %>%
rvest::html_nodes("[class='gt_row gt_center']") %>%
rvest::html_text() %>%
.[1:10] %>%
expect_equal(
c(
"1.111e-01", "apricot", "one", "2015-01-15", "13:35",
"2018-01-01 02:22", "49.950", "row_1", "grp_a", "test"
)
)
})
test_that("Any shared names in `rowname_col` and `groupname_col` will be disallowed", {
# Expect an error if there are any shared names across `rowname_col`
# and `groupname_col`
expect_error(
exibble %>%
gt(rowname_col = "row", groupname_col = "row")
)
expect_error(
exibble %>%
gt(rowname_col = "group", groupname_col = "group")
)
expect_error(
exibble %>%
gt(rowname_col = "rowname", groupname_col = "rowname")
)
expect_error(
exibble %>%
dplyr::group_by(group) %>%
gt(rowname_col = "group")
)
expect_error(
exibble %>%
dplyr::group_by(date, row) %>%
gt(rowname_col = "row")
)
})
test_that("Escapable characters in rownames are handled correctly in each output context", {
tbl <-
data.frame(
row_names = c("<em>row_html</em>", "$row_latex", "{row_rtf}"),
column_1 = c("<em>html</em>", "$latex", "{rtf}"),
column_2 = c("html", "latex", "rtf"),
row.names = "row_names",
stringsAsFactors = FALSE
)
# Expect that the stub and body rows are escaped correctly
# when rendered as HTML
# Using the data frame and setting its rownames to be the stub
expect_match( # stub from data frame's row names
gt(tbl, rownames_to_stub = TRUE) %>%
render_as_html(),
"<tr><td class=\"gt_row gt_left gt_stub\"><em>row_html</em></td>",
fixed = TRUE
)
expect_match( # `column_1`
gt(tbl, rownames_to_stub = TRUE) %>%
render_as_html(),
"<td class=\"gt_row gt_left\"><em>html</em></td>",
fixed = TRUE
)
expect_match( # `column_2`
gt(tbl, rownames_to_stub = TRUE) %>%
render_as_html(),
"<td class=\"gt_row gt_left\">html</td>",
fixed = TRUE
)
# Using a tibble (removes row names) and setting `column_1` as the stub
expect_match( # stub from `column_1`
gt(dplyr::as_tibble(tbl), rowname_col = "column_1") %>%
render_as_html(),
"<tr><td class=\"gt_row gt_left gt_stub\"><em>html</em></td>",
fixed = TRUE
)
expect_match( # `column_2`
gt(dplyr::as_tibble(tbl), rowname_col = "column_1") %>%
render_as_html(),
"<td class=\"gt_row gt_left\">html</td>",
fixed = TRUE
)
# Expect that the stub and body rows are escaped correctly
# when rendered as LaTeX
# Expect that the stub and body rows are escaped correctly
# when rendered as LaTeX
expect_match(
gt(tbl, rownames_to_stub = TRUE) %>%
as_latex() %>% as.character(),
"\\$row\\_latex & \\$latex & latex \\\\ ",
fixed = TRUE
)
# Using a tibble (removes row names) and setting `column_1` as the stub
expect_match(
gt(dplyr::as_tibble(tbl), rowname_col = "column_1") %>%
as_latex() %>% as.character(),
"\\$latex & latex \\\\ ",
fixed = TRUE
)
# Expect that the stub and body rows are escaped correctly
# when rendered as RTF
# Using the data frame and setting its rownames to be the stub
expect_match( # stub from data frame's row names
gt(tbl, rownames_to_stub = TRUE) %>%
as_rtf() %>% as.character(),
"\\intbl {\\f0 {\\f0\\fs20 \\'7brow_rtf\\'7d}}\\cell",
fixed = TRUE
)
expect_match( # `column_1`
gt(tbl, rownames_to_stub = TRUE) %>%
as_rtf() %>% as.character(),
"\\intbl {\\f0 {\\f0\\fs20 \\'7brtf\\'7d}}\\cell",
fixed = TRUE
)
expect_match( # `column_2`
gt(tbl, rownames_to_stub = TRUE) %>%
as_rtf() %>% as.character(),
"\\intbl {\\f0 {\\f0\\fs20 rtf}}\\cell",
fixed = TRUE
)
# Using a tibble (removes row names) and setting `column_1` as the stub
expect_match( # stub from `column_1`
gt(dplyr::as_tibble(tbl), rowname_col = "column_1") %>%
as_rtf() %>% as.character(),
"\\intbl {\\f0 {\\f0\\fs20 \\'7brtf\\'7d}}\\cell",
fixed = TRUE
)
expect_match( # `column_2`
gt(dplyr::as_tibble(tbl), rowname_col = "column_1") %>%
as_rtf() %>% as.character(),
"\\intbl {\\f0 {\\f0\\fs20 rtf}}\\cell",
fixed = TRUE
)
})