File: row_spec.R

package info (click to toggle)
r-cran-kableextra 1.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,400 kB
  • sloc: javascript: 579; makefile: 2
file content (352 lines) | stat: -rw-r--r-- 13,663 bytes parent folder | download
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
#' Specify the look of the selected row
#'
#' @description This function allows users to select a row and then specify
#' its look. It can also specify the format of the header row when `row` = 0.
#'
#' @param kable_input Output of `knitr::kable()` with `format` specified
#' @param row A numeric value or vector indicating which row(s) to be selected. You don't
#' need to count in header rows or group labeling rows.
#' @param bold A T/F value to control whether the text of the selected row
#' need to be bolded.
#' @param italic A T/F value to control whether the text of the selected row
#' need to be emphasized.
#' @param monospace A T/F value to control whether the text of the selected row
#' need to be monospaced (verbatim)
#' @param underline A T/F value to control whether the text of the selected row
#' need to be underlined
#' @param strikeout A T/F value to control whether the text of the selected row
#' need to be struck out.
#' @param color A character string for row text color. For example, "red" or
#' "#BBBBBB".
#' @param background A character string for row background color. Here please
#' pay attention to the differences in color codes between HTML and LaTeX.
#' @param align A character string for cell alignment. For HTML, possible values could
#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`
#' while for LaTeX, you can only choose from `l`, `c` & `r`.
#' @param font_size A numeric input for font size. For HTML, you can also use
#' options including `xx-small`, `x-small`, `small`, `medium`, `large`,
#' `x-large`, `xx-large`, `smaller`, `larger`, `initial` and `inherit`.
#' @param angle 0-360, degree that the text will rotate.
#' @param extra_css Extra css text to be passed into the cells of the row. Note
#' that it's not for the whole row.
#' @param hline_after T/F. A replicate of `hline.after` in xtable. It
#' adds a hline after the row
#' @param extra_latex_after Extra LaTeX text to be added after the row. Similar
#' with `add.to.row` in xtable
#'
#' @examples
#' \dontrun{
#' x <- knitr::kable(head(mtcars), "html")
#' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
#' }
#'
#' @export
row_spec <- function(kable_input, row,
                     bold = FALSE, italic = FALSE, monospace = FALSE,
                     underline = FALSE, strikeout = FALSE,
                     color = NULL, background = NULL, align = NULL,
                     font_size = NULL, angle = NULL, extra_css = NULL,
                     hline_after = FALSE, extra_latex_after = NULL) {
  if (!is.numeric(row)) {
    stop("row must be numeric. ")
  }
  kable_format <- attr(kable_input, "format")
  if (kable_format %in% c("pipe", "markdown")) {
    kable_input <- md_table_parser(kable_input)
    kable_format <- attr(kable_input, "format")
  }
  if (!kable_format %in% c("html", "latex")) {
    warning("Please specify format in kable. kableExtra can customize either ",
            "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
            "for details.")
    return(kable_input)
  }
  if (kable_format == "html") {
    return(row_spec_html(kable_input, row, bold, italic, monospace,
                         underline, strikeout,
                         color, background, align, font_size, angle,
                         extra_css))
  }
  if (kable_format == "latex") {
    return(row_spec_latex(kable_input, row, bold, italic, monospace,
                          underline, strikeout,
                          color, background, align, font_size, angle,
                          hline_after, extra_latex_after))
  }
}

row_spec_html <- function(kable_input, row, bold, italic, monospace,
                          underline, strikeout,
                          color, background, align, font_size, angle,
                          extra_css) {
  kable_attrs <- attributes(kable_input)
  important_nodes <- read_kable_as_xml(kable_input)
  body_node <- important_nodes$body
  kable_xml <- important_nodes$table

  if (!is.null(align)) {
    if (align %in% c("l", "c", "r")) {
      align <- switch(align, r = "right", c = "center", l = "left")
    }
  }

  if (0 %in% row) {
    kable_thead <- xml_tpart(kable_xml, "thead")
    original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
    for (theader_i in 1:length(xml_children(original_header_row))) {
      target_header_cell <- xml_child(original_header_row, theader_i)
      xml_cell_style(target_header_cell, bold, italic, monospace,
                     underline, strikeout, color, background,
                     align, font_size, angle, extra_css)
    }
    row <- row[row != 0]
  }

  if (length(row) != 0) {
    kable_tbody <- xml_tpart(kable_xml, "tbody")

    group_header_rows <- attr(kable_input, "group_header_rows")
    if (!is.null(group_header_rows)) {
      row <- positions_corrector(row, group_header_rows,
                                 length(xml_children(kable_tbody)))
    }

    for (j in row) {
      target_row <- xml_child(kable_tbody, j)
      for (i in 1:length(xml_children(target_row))) {
        target_cell <- xml_child(target_row, i)
        xml_cell_style(target_cell, bold, italic, monospace,
                       underline, strikeout, color, background,
                       align, font_size, angle, extra_css)
      }
    }
  }

  out <- as_kable_xml(body_node)
  attributes(out) <- kable_attrs
  if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
  return(out)
}

xml_cell_style <- function(x, bold, italic, monospace,
                           underline, strikeout, color, background,
                           align, font_size, angle, extra_css) {
  if (is.na(xml_attr(x, "style"))) {
    xml_attr(x, "style") <- ""
  }
  if (bold) {
    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
                                   "font-weight: bold;")
  }
  if (italic) {
    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
                                   "font-style: italic;")
  }
  if (monospace) {
    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
                                   "font-family: monospace;")
  }
  if (underline) {
    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
                                   "text-decoration: underline;")
  }
  if (strikeout) {
    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
                                   "text-decoration: line-through;")
  }
  if (!is.null(color)) {
    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
                                   "color: ", html_color(color), " !important;")
  }
  if (!is.null(background)) {
    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
                                   "background-color: ",
                                   html_color(background), " !important;")
  }
  if (!is.null(align)) {
    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
                                   "text-align: ", align, ";")
  }
  if (!is.null(font_size)) {
    if (is.numeric(font_size)) font_size <- paste0(font_size, "px")
    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
                                   "font-size: ", font_size, ";")
  }
  if (!is.null(angle)) {
    xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
                                   "-webkit-transform: rotate(", angle,
                                   "deg); -moz-transform: rotate(", angle,
                                   "deg); -ms-transform: rotate(", angle,
                                   "deg); -o-transform: rotate(", angle,
                                   "deg); transform: rotate(", angle,
                                   "deg);")
  }
  if (!is.null(extra_css)) {
    xml_attr(x, "style") <- paste0(xml_attr(x, "style"), enc2utf8(extra_css))
  }
  return(x)
}

row_spec_latex <- function(kable_input, row, bold, italic, monospace,
                           underline, strikeout,
                           color, background, align, font_size, angle,
                           hline_after, extra_latex_after) {
  table_info <- magic_mirror(kable_input)
  out <- solve_enc(kable_input)

  if (table_info$duplicated_rows) {
    dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
    out <- dup_fx_out[[1]]
    table_info <- dup_fx_out[[2]]
  }

  row <- row + table_info$position_offset
  for (i in row) {
    target_row <- table_info$contents[i]
    new_row <- latex_new_row_builder(target_row, table_info,
                                     bold, italic, monospace,
                                     underline, strikeout,
                                     color, background, align, font_size, angle,
                                     hline_after, extra_latex_after)
    temp_sub <- ifelse(i == 1 & (table_info$tabular == "longtable" |
                                   !is.null(table_info$repeat_header_latex)),
                       gsub, sub)
    if (length(new_row) == 1) {
      # fixed=TRUE is safer but does not always work
      regex <- paste0("\\Q", target_row, "\\E")
      if (grepl(regex, out)) {
        out <- temp_sub(regex, new_row, out, perl = TRUE)
      } else {
        out <- temp_sub(paste0(target_row, "\\\\\\\\"),
                        paste0(new_row, "\\\\\\\\"), out, perl = TRUE)
      }
      table_info$contents[i] <- new_row
    } else {
      # fixed=TRUE is safer but does not always work
      regex <- paste0("\\Q", target_row, "\\E")
      if (any(grepl(regex, out))) {
        out <- temp_sub(regex,
          paste(new_row, collapse = ""), out, perl = TRUE)
      } else {
        out <- temp_sub(paste0(target_row, "\\\\\\\\"),
                    paste(new_row, collapse = ""), out, perl = TRUE)
      }
      table_info$contents[i] <- new_row[1]
    }
  }

  out <- structure(out, format = "latex", class = "knitr_kable")
  attr(out, "kable_meta") <- table_info
  return(out)
}

latex_new_row_builder <- function(target_row, table_info,
                                  bold, italic, monospace,
                                  underline, strikeout,
                                  color, background, align, font_size, angle,
                                  hline_after, extra_latex_after) {
  new_row <- latex_row_cells(target_row)
  if (bold) {
    new_row <- lapply(new_row, function(x) {
      paste0("\\\\textbf\\{", x, "\\}")
    })
  }
  if (italic) {
    new_row <- lapply(new_row, function(x) {
      paste0("\\\\em\\{", x, "\\}")
    })
  }
  if (monospace) {
    new_row <- lapply(new_row, function(x) {
      paste0("\\\\ttfamily\\{", x, "\\}")
    })
  }
  if (underline) {
    new_row <- lapply(new_row, function(x) {
      paste0("\\\\underline\\{", x, "\\}")
    })
  }
  if (strikeout) {
    new_row <- lapply(new_row, function(x) {
      paste0("\\\\sout\\{", x, "\\}")
    })
  }
  if (!is.null(color)) {
    if (table_info$tabular == "tabu") {
      warning("Setting full_width = TRUE will turn the table into a tabu ",
              "environment where colors are not really easily configable ",
              "with this package. Please consider turn off full_width.")
    }
    new_row <- lapply(new_row, function(x) {
      x <- clear_color_latex(x)
      paste0("\\\\textcolor", latex_color(color), "\\{", x, "\\}")
    })
  }
  if (!is.null(background)) {
    if (table_info$tabular == "tabu") {
      warning("Setting full_width = TRUE will turn the table into a tabu ",
              "environment where colors are not really easily configable ",
              "with this package. Please consider turn off full_width.")
    }
    new_row <- lapply(new_row, function(x) {
      x <- clear_color_latex(x, background = TRUE)
      paste0("\\\\cellcolor", latex_color(background), "\\{", x, "\\}")
    })
  }
  if (!is.null(font_size)) {
    new_row <- lapply(new_row, function(x) {
      paste0("\\\\begingroup\\\\fontsize\\{", font_size, "\\}\\{",
             as.numeric(font_size) + 2,
             "\\}\\\\selectfont ", x, "\\\\endgroup")})
  }
  if (!is.null(align)) {
    if (!is.null(table_info$column_width)) {
      p_align <- switch(align,
                        "l" = "\\\\raggedright\\\\arraybackslash",
                        "c" = "\\\\centering\\\\arraybackslash",
                        "r" = "\\\\raggedleft\\\\arraybackslash")
      align <- rep(align, table_info$ncol)
      p_cols <- as.numeric(sub("column_", "", names(table_info$column_width)))
      for (i in 1:length(p_cols)) {
        align[p_cols[i]] <- paste0("\\>\\{", p_align, "\\}p\\{",
                                   table_info$column_width[[i]], "\\}")
      }
    }
    new_row <- lapply(new_row, function(x) {
      paste0("\\\\multicolumn\\{1\\}\\{", align, "\\}\\{", x, "\\}")
    })
  }

  if (!is.null(angle)) {
    new_row <- lapply(new_row, function(x) {
      paste0("\\\\rotatebox\\{", angle, "\\}\\{", x, "\\}")
    })
  }

  new_row <- paste(unlist(new_row), collapse = " & ")

  # if (!is.null(background)) {
  #   new_row <- paste0("\\\\rowcolor", latex_color(background), "  ", new_row)
  # }

  if (!hline_after & is.null(extra_latex_after)) {
    return(new_row)
  } else {
    latex_after <- "\\\\\\\\"
    if (hline_after) {
      if (table_info$booktabs) {
        latex_after <- paste0(latex_after, "\n\\\\midrule")
      } else {
        latex_after <- paste0(latex_after, "\n\\\\hline")
      }
    }
    if (!is.null(extra_latex_after)) {
      latex_after <- paste0(latex_after, "\n",
                            regex_escape(extra_latex_after,
                                         double_backslash = TRUE))
    }
    return(c(new_row, latex_after))
  }
}