File: htmlTable_example.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 (82 lines) | stat: -rw-r--r-- 3,379 bytes parent folder | download | duplicates (2)
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
library(magrittr)

# Basic example
output <- matrix(1:4,
                 ncol = 2,
                 dimnames = list(list("Row 1", "Row 2"),
                                 list("Column 1", "Column 2")))
htmlTable(output)
invisible(readline(prompt = "Press [enter] to continue"))

# An advanced output
output <- matrix(ncol = 6, nrow = 8)

for (nr in 1:nrow(output)) {
  for (nc in 1:ncol(output)) {
    output[nr, nc] <-
      paste0(nr, ":", nc)
  }
}

output %>% addHtmlTableStyle(align = "r",
                             col.columns = c(rep("none", 2),
                                             rep("#F5FBFF", 4)),
                             col.rgroup = c("none", "#F7F7F7"),
                             css.cell = "padding-left: .5em; padding-right: .2em;") %>%
  htmlTable(header =  paste(c("1st", "2nd",
                              "3rd", "4th",
                              "5th", "6th"),
                            "hdr"),
            rnames = paste(c("1st", "2nd",
                             "3rd",
                             paste0(4:8, "th")),
                           "row"),
            rgroup = paste("Group", LETTERS[1:3]),
            n.rgroup = c(2,4,nrow(output) - 6),
            cgroup = rbind(c("", "Column spanners", NA),
                           c("", "Cgroup 1", "Cgroup 2&dagger;")),
            n.cgroup = rbind(c(1,2,NA),
                             c(2,2,2)),
            caption = "Basic table with both column spanners (groups) and row groups",
            tfoot = "&dagger; A table footer commment",
            cspan.rgroup = 2)
invisible(readline(prompt = "Press [enter] to continue"))

# An advanced empty table
suppressWarnings({
  matrix(ncol = 6,
         nrow = 0) %>%
    addHtmlTableStyle(col.columns = c(rep("none", 2),
                                      rep("#F5FBFF", 4)),
                      col.rgroup = c("none", "#F7F7F7"),
                      css.cell = "padding-left: .5em; padding-right: .2em;") %>%
    htmlTable(align = "r",
              header =  paste(c("1st", "2nd",
                                "3rd", "4th",
                                "5th", "6th"),
                              "hdr"),
              cgroup = rbind(c("", "Column spanners", NA),
                             c("", "Cgroup 1", "Cgroup 2&dagger;")),
              n.cgroup = rbind(c(1,2,NA),
                               c(2,2,2)),
              caption = "Basic empty table with column spanners (groups) and ignored row colors",
              tfoot = "&dagger; A table footer commment",
              cspan.rgroup = 2)
})
invisible(readline(prompt = "Press [enter] to continue"))

# An example of how to use the css.cell for header styling
simple_output <- matrix(1:4, ncol = 2)

simple_output %>%
  addHtmlTableStyle(css.cell = rbind(rep("background: lightgrey; font-size: 2em;",
                                         times = ncol(simple_output)),
                                     matrix("",
                                            ncol = ncol(simple_output),
                                            nrow = nrow(simple_output)))) %>%
  htmlTable(header = LETTERS[1:2])
invisible(readline(prompt = "Press [enter] to continue"))

# See vignette("tables", package = "htmlTable")
# for more examples, also check out tidyHtmlTable() that manages
# the group arguments for you through tidy-select syntax