-
Notifications
You must be signed in to change notification settings - Fork 197
/
test-l_fmt_missing.R
73 lines (62 loc) · 2.31 KB
/
test-l_fmt_missing.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
context("LaTeX -- Ensuring that the `fmt_missing()` function works as expected")
test_that("the `fmt_missing()` function works correctly", {
# Create an input data frame with two columns, both numeric
data_tbl <-
data.frame(
num_1 = c(NA, 74, NA, 93, NA, 76, NA),
num_2 = c(34, 74, 23, 93, 35, 76, 57),
stringsAsFactors = FALSE)
# Create a `tbl_latex` object with `gt()` and the `data_tbl` dataset
tbl_latex <- gt(data_tbl)
#
# Use `fmt_missing()` with various options
#
expect_equal(
(tbl_latex %>%
fmt_missing(columns = "num_1") %>%
render_formats_test(context = "latex"))[["num_1"]],
c("---", "74", "---", "93", "---", "76", "---"))
expect_equal(
(tbl_latex %>%
fmt_missing(columns = "num_1", missing_text = "--") %>%
render_formats_test(context = "latex"))[["num_1"]],
c("--", "74", "--", "93", "--", "76", "--"))
expect_equal(
(tbl_latex %>%
fmt_missing(columns = "num_1", missing_text = "a") %>%
render_formats_test(context = "latex"))[["num_1"]],
c("a", "74", "a", "93", "a", "76", "a"))
expect_equal(
(tbl_latex %>%
fmt_missing(columns = "num_2") %>%
render_formats_test(context = "latex"))[["num_2"]],
c("34", "74", "23", "93", "35", "76", "57"))
expect_equal(
(tbl_latex %>%
fmt_missing(columns = "num_1", rows = num_2 < 50) %>%
render_formats_test(context = "latex"))[["num_1"]],
c("---", "74", "---", "93", "---", "76", "NA"))
# Format columns with `fmt_number()` then use
# `fmt_missing()` on all columns (the two functions
# shouldn't wipe out formatting on cells)
expect_equal(
(tbl_latex %>%
fmt_number(
columns = everything(),
decimals = 3
) %>%
fmt_missing(columns = everything()) %>%
render_formats_test(context = "latex"))[["num_1"]],
c("---", "$74.000$", "---", "$93.000$", "---", "$76.000$", "---"))
# Reverse the ordering: use `fmt_missing()` first
# then `fmt_number()`; expect the same output as before
expect_equal(
(tbl_latex %>%
fmt_missing(columns = everything()) %>%
fmt_number(
columns = everything(),
decimals = 3
) %>%
render_formats_test(context = "latex"))[["num_1"]],
c("---", "$74.000$", "---", "$93.000$", "---", "$76.000$", "---"))
})