File: htmlTable_vtests.R

package info (click to toggle)
r-cran-htmltable 2.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,600 kB
  • sloc: javascript: 6,797; makefile: 2
file content (211 lines) | stat: -rw-r--r-- 7,576 bytes parent folder | download | duplicates (3)
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
options(htmlTable.pretty_indentation = TRUE)
mx <- paste("value ", 1:6) %>%
  matrix(ncol = 3)
colnames(mx) <- c("A", "B", "C") %>%
  paste("header", .)
rownames(mx) <- letters[1:2]
## col.rgroup does not break css.group
setHtmlTableTheme(theme = "Google")
mx %>%
  addHtmlTableStyle(css.rgroup = "font-weight:900; background-color:#f2f2f2;",
                    align = "|l|r|c|r|",
                    spacer.celltype = "skip") %>%
  htmlTable(n.rgroup = c(2),
            rgroup = c("Nice!"),
            n.cgroup = list(c(1, 2),
                            c(2,1)),
            cgroup = list(c("1:1", "1:2-3"),
                          c("2:1-2", "2:3")))

mx %>%
  addHtmlTableStyle(css.cgroup = "font-weight:900; background-color:#f2f2f2; vertical-align:middle;",
                    align = "|l|r|c|r|",
                    spacer.celltype = "single_empty") %>%
  htmlTable(n.rgroup = c(2),
            rgroup = c("Nice!"),
            n.cgroup = list(c(1, 2),
                            c(2,1)),
            cgroup = list(c("1:1", "1:2-3"),
                          c("2:1-2", "2:3")))

mx %>%
  addHtmlTableStyle(align = "|l|r|c|r|",
                    align.header = "|c|c|c|",
                    spacer.celltype = "double") %>%
  htmlTable(n.rgroup = c(2),
            rgroup = c("Nice!"),
            n.cgroup = list(c(1, 2),
                            c(2,1)),
            cgroup = list(c("1:1", "1:2-3"),
                          c("2:1-2", "2:3")))


mx %>%
  set_colnames(c("A<br />first", "B", "C") %>%
                 paste("header", .)) %>%
  addHtmlTableStyle(align = "|l|r|c|r|",
                    spacer.celltype = "double") %>%
  htmlTable(n.rgroup = c(2),
            rgroup = c("Vertical alignment in header check"),
            n.cgroup = list(c(1, 2),
                            c(2,1)),
            cgroup = list(c("1:1<br />vertical check", "1:2-3"),
                          c("2:1-2", "2:3")))

colnames(mx) <- NULL
htmlTable(mx)
htmlTable(mx[1,,drop = FALSE])
htmlTable(mx, n.rgroup = 2, rgroup = "A")
htmlTable(mx, tspanner = "AA", n.tspanner = 2,
          n.rgroup = 2, rgroup = "A")
htmlTable(mx, tspanner = "AA", n.tspanner = 2,
          padding.tspanner = "&nbsp;&nbsp;",
          n.rgroup = 2, rgroup = "A")
htmlTable(mx, tspanner = "AA", n.tspanner = 2)

htmlTable(mx, n.rgroup = 2, rgroup = "A", padding.rgroup = "")


# This will cause the table to look strange
# but forcing &gt;/&lt; is a bigger constraint
# that may be undesirable for more advanced users.
mx[1,1] <- "< = &lt;"
mx[1,2] <- "2<sup>2</sup>"
mx[1,3] <- "<span style=\"font-weight: 900\">3</span>"
mx[2,1] <- "<td>"
htmlTable(mx)


mx <- matrix(1:9, ncol = 3)
colnames(mx) <- LETTERS[1:3]
rownames(mx) <- letters[1:3]

mx_3_times <- rbind(mx,
                    mx,
                    mx)
htmlTable(mx_3_times,
          css.tspanner.sep = "border-top: 2px solid red;",
          rgroup = rep(c("Group a", "Group b and c"), times = 3),
          n.rgroup = rep(c(1,2), times = 3),
          tspanner = c("First", "Second", "Third"),
          n.tspanner = rep(nrow(mx), times = 3),
          rowlabel = '',
          col.rgroup = c('white','lightblue1'),
          tfoot = "Some footer text",
          caption = "Caption text")

htmlTable(mx_3_times,
          css.tspanner.sep = c("border-top: 2px solid red;",
                             "border-top: 2px solid blue;"),
          rgroup = rep(c("Group a", "Group b and c"), times = 3),
          n.rgroup = rep(c(1,2), times = 3),
          tspanner = c("First", "Second", "Third"),
          n.tspanner = rep(nrow(mx), times = 3),
          rowlabel = '',
          col.rgroup = c('white','lightblue1'),
          tfoot = "Some footer text",
          caption = "Caption text")


htmlTable(mx_3_times,
          css.tspanner.sep = c("border-top: 2px solid red;",
                             "border-top: 2px solid blue;"),
          rgroup = rep(c("Group a", "Group b and c"), times = 3),
          n.rgroup = rep(c(1,2), times = 3),
          tspanner = c("First", "Second", "Third"),
          n.tspanner = rep(nrow(mx), times = 3),
          rowlabel = '',
          col.rgroup = c('white','lightblue1'),
          col.columns = c('none','#CCCCCC'),
          tfoot = "Some footer text",
          caption = "Caption text")

htmlTable(mx_3_times,
          css.tspanner.sep = c("border-top: 2px solid red;",
                                 "border-top: 12px solid blue;"),
          rgroup = rep(c("Group a", "Group b and c"), times = 3),
          n.rgroup = rep(c(1,2), times = 3),
          tspanner = c("First", "Second", "Third"),
          n.tspanner = rep(nrow(mx), times = 3),
          rowlabel = '',
          tfoot = "Some footer text",
          caption = "Caption text")


htmlTable(mx_3_times,
          css.tspanner = "color: purple; font-weight: bold;",
          css.tspanner.sep = "border-top: 2px solid red;",
          rgroup = rep(c("Group a", "Group b and c"), times = 3),
          n.rgroup = rep(c(1,2), times = 3),
          tspanner = c("First", "Second", "Third"),
          n.tspanner = rep(nrow(mx), times = 3),
          rowlabel = '',
          tfoot = "Some footer text",
          caption = "Caption text")

htmlTable(mx_3_times,
          padding.tspanner = "+",
          padding.rgroup = "-",
          css.tspanner = "color: purple; font-weight: bold;",
          css.tspanner.sep = "border-top: 2px solid red;",
          rgroup = rep(c("Group a", "Group b and c"), times = 3),
          n.rgroup = rep(c(1,2), times = 3),
          tspanner = c("First", "Second", "Third"),
          n.tspanner = rep(nrow(mx), times = 3),
          rowlabel = '',
          tfoot = "&dagger; Some footnote
          &Dagger; Another footnote",
          caption = "Caption text")

rbind(
  `Group A` = c(20, 380),
  `Group B` = c(110, 1230),
  `Group C` = c(2, 56),
  `Group D` = c(17, 33),
  `Group A` = c(40, 360),
  `Group B` = c(230, 1100),
  `Group C` = c(8, 50),
  `Group D` = c(10, 40)
) %>%
  apply(1, function(x) {
    sapply(x, function(count) c(
      txtInt(count),
      sprintf("(%s)", txtRound(count/sum(x) * 100, 1)))) %>%
      c(txtInt(sum(x)), .)
  }) %>%
  t %>%
  htmlTable(header = c("Total", rep(c("No", "(%)"), times = 2)),
            n.cgroup = list(c(1, 2, 2)),
            cgroup = list(c("", "Cases", "Controls")),
            rgroup = rep(c("Aspirin", "Intermittent compression"), times = 2),
            n.rgroup = rep(2, times = 4),
            tspanner = c("First experiment", "Second experiment"),
            n.tspanner = c(2),
            align = "r",
            caption = "Extremely fake data")


library(tidyverse)
mtcars %>%
  as_tibble(rownames = "rnames") %>%
  pivot_longer(names_to = "per_metric",
               cols = c(hp, mpg, qsec)) %>%
  group_by(cyl, gear, per_metric) %>%
  summarise(Mean = round(mean(value), 1),
            SD = round(sd(value), 1),
            Min = round(min(value), 1),
            Max = round(max(value), 1),
            .groups = 'drop') %>%
  pivot_longer(names_to = "summary_stat",
               cols = c(Mean, SD, Min, Max)) %>%
  ungroup() %>%
  mutate(gear = paste(gear, "Gears"),
         cyl = paste(cyl, "Cylinders")) %>%
  arrange(per_metric, summary_stat) %>%
  addHtmlTableStyle(align = "r") %>%
  tidyHtmlTable(header = gear,
                cgroup = cyl,
                rnames = summary_stat,
                rgroup = per_metric,
                skip_removal_warning = TRUE,
                caption = "A full example of how to apply the tidyverse workflow to generate a table")