-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboldlatex.R
97 lines (97 loc) · 2.89 KB
/
boldlatex.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
printbold <- function(x,
which = NULL,
each = c("column", "row"),
max = TRUE,
abs = FALSE,
NA.string = "",
type = c("latex", "html"),
sanitize.text.function = force,
sanitize.rownames.function = NULL,
sanitize.colnames.function = NULL,
...) {
stopifnot(inherits(x, "xtable"))
each <- match.arg(each)
type <- match.arg(type)
digits <- rep(digits(x), length = ncol(x) + 1)
if (!is.null(which)) {
stopifnot(nrow(which) == nrow(x))
stopifnot(ncol(which) == ncol(x))
boldmatrix <- which
} else {
boldmatrix <- matrix(FALSE, ncol = ncol(x), nrow = nrow(x))
## round values before calculating max/min to avoid trivial diffs
for (i in 1:ncol(x)) {
if (!is.numeric(x[, i]))
next
if (digits[i + 1] != -1) {
x[, i] <- round(x[, i], digits = digits[i + 1])
}
}
if (each == "column") {
max <- rep(max, length = ncol(x))
abs <- rep(abs, length = ncol(x))
for (i in 1:ncol(x)) {
if (abs[i]) {
xi <- abs(x[, i])
} else {
xi <- x[, i]
}
if (!is.numeric(xi))
next
if (is.na(max[i]))
next
imax <- max(xi, na.rm = TRUE)
if (!max[i])
imax <- min(xi, na.rm = TRUE)
boldmatrix[xi == imax, i] <- TRUE
}
} else if (each == "row") {
max <- rep(max, length = nrow(x))
for (i in 1:nrow(x)) {
xi <- x[i,]
ok <- sapply(xi, is.numeric)
if (!any(ok))
next
if (is.na(max[i]))
next
imax <- max(unlist(xi[ok]), na.rm = TRUE)
if (!max[i])
imax <- min(unlist(xi[ok]), na.rm = TRUE)
whichmax <- sapply(xi, identical, imax)
boldmatrix[i, whichmax] <- TRUE
}
}
}
## need to convert to character
## only support per-column formats, not cell formats
display <- rep(display(x), length = ncol(x) + 1)
for (i in 1:ncol(x)) {
if (!is.numeric(x[, i]))
next
ina <- is.na(x[, i])
if (digits[i + 1] != -1) {
x[, i] <- formatC(x[, i], digits = digits[i + 1],
format = display[i + 1])
} else {
x[, i] <- formatC(x[, i], format = "e", digits = 1)
}
x[ina, i] <- NA.string
display(x)[i + 1] <- "s"
## embolden
yes <- boldmatrix[, i]
if (type == "latex") {
x[yes, i] <- paste("\\textbf{", x[yes, i], "}", sep = "")
} else {
x[yes, i] <- paste("<strong>", x[yes, i], "</strong>", sep = "")
}
}
print(
x,
...,
type = type,
NA.string = NA.string,
sanitize.text.function = sanitize.text.function,
sanitize.rownames.function = sanitize.rownames.function,
sanitize.colnames.function = sanitize.colnames.function
)
}