-
Notifications
You must be signed in to change notification settings - Fork 175
/
utils.R
189 lines (151 loc) · 5.02 KB
/
utils.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
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
# options(warn = -1)
# TODO: Add checks for code/computer output wider than 80 columns
library <- function(...) {
suppressPackageStartupMessages(base::library(...))
}
# Need to load some packages before tidyverse because of conflicts
library(igraph)
library(MASS)
library(ggplot2)
library(dplyr)
options(width = 77)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
cache = TRUE,
fig.show = "hold",
fig.align = "center",
fig.width = 5,
fig.height = 4,
out.width = NULL,
print_df_rows = c(3, 3),
print_df_digits = NULL,
print_tbl_rows = 6
)
# Seems to be necessary for captions for multiple images in a single Figure.
if (identical(knitr:::pandoc_to(), 'latex')) {
knitr::knit_hooks$set(plot = knitr::hook_plot_tex)
}
knitr::knit_hooks$set(small.mar = function(before, options, envir) {
if (before) par(mar = c(4.5, 4.5, 1, .5)) # smaller margin on top and right
})
# As of R 3.5, we need to register knit_print methods using registerS3method.
# https://github.com/yihui/knitr/issues/1580
#
# This is borrowed from: https://github.com/klutometis/roxygen/issues/623
register_s3_method <- function(pkg, generic, class, fun = NULL) {
stopifnot(is.character(pkg), length(pkg) == 1)
envir <- asNamespace(pkg)
stopifnot(is.character(generic), length(generic) == 1)
stopifnot(is.character(class), length(class) == 1)
if (is.null(fun)) {
fun <- get(paste0(generic, ".", class), envir = parent.frame())
}
stopifnot(is.function(fun))
if (pkg %in% loadedNamespaces()) {
registerS3method(generic, class, fun, envir = envir)
}
# Always register hook in case package is later unloaded & reloaded
setHook(
packageEvent(pkg, "onLoad"),
function(...) {
registerS3method(generic, class, fun, envir = envir)
}
)
}
# Round numeric columns in a data frame to some number of digits after decimal.
round_df <- function(df, digits) {
is.num <- vapply(df, is.numeric, TRUE)
df[is.num] <- lapply(df[is.num], round, digits = digits)
df
}
knit_print_data.frame <- function(x, ...,
rows = knitr::opts_current$get("print_df_rows"),
digits_ = knitr::opts_current$get("print_df_digits")
) {
# If the data frame has up to `rows` plus two extra, just print the data
# frame.
if (nrow(x) <= sum(rows) + 2) {
print(x)
return()
}
# By default, print first 3 and last 3 rows.
rownums <- c(seq(from = 1, length.out = rows[1]),
seq(to = nrow(x), length.out = rows[2]))
# If digits_ is specified, round to that number of digits. It's called
# `digits_` so it doesn't clash with the `digits` parameter for
# print.data.frame.
if (!is.null(digits_)) {
x <- round_df(x, digits = digits_)
}
# Need to print the head and tail of data frame in one go, so that
# alignment is correct.
output <- capture.output(
print(x[rownums, , drop = FALSE], ...)
)
if (nrow(x) > sum(rows)) {
output <- c(
head(output, -rows[2]),
paste0(
" ...<",
format(nrow(x) - sum(rows), big.mark = ","),
" more rows>..."
),
tail(output, rows[2])
)
}
cat(output, sep = "\n")
}
# This will need tweaking to work with very long vectors which do not finish printing
knit_print_vector <- function(x, ..., rows = knitr::opts_current$get("print_df_rows")) {
output <- capture.output(
print(x, ...)
)
# If the printed output has up to `rows` plus two extra, just print it.
if (length(output) <= sum(rows) + 2) {
cat(output, sep = "\n")
return()
}
if (length(output) > sum(rows)) {
output <- c(
head(output, rows[1]),
paste0(
" ..."
),
tail(output, rows[2])
)
}
cat(output, sep = "\n")
}
knit_print_ts <- function(x, ..., rows = knitr::opts_current$get("print_df_rows")) {
# Need one extra row for header
knit_print_vector(x, ..., rows = c(rows[1] + 1, rows[2]))
}
# Customized for Creating a Mosaic Plot recipe, with UCBAdmissions
knit_print_table <- function(x, ...) {
txt <- capture.output(base::print.table(x, ...))
if (length(txt) > 14) {
cat(txt[1:14], sep = "\n")
cat(" ... with", length(txt)-1, "more lines of text\n")
} else{
cat(txt, sep = "\n")
}
invisible()
}
knit_print_tbl <- function(x, ..., maxrows = knitr::opts_current$get("print_tbl_rows")) {
print(x, ..., n = maxrows)
}
# Need this explicit registration step because of some change in R 3.5
register_s3_method("knitr", "knit_print", "data.frame", knit_print_data.frame)
register_s3_method("knitr", "knit_print", "matrix", knit_print_data.frame)
register_s3_method("knitr", "knit_print", "character", knit_print_vector)
register_s3_method("knitr", "knit_print", "ts", knit_print_ts)
register_s3_method("knitr", "knit_print", "table", knit_print_table)
register_s3_method("knitr", "knit_print", "grouped_df", knit_print_tbl)
register_s3_method("knitr", "knit_print", "tbl_df", knit_print_tbl)
register_s3_method("knitr", "knit_print", "tbl", knit_print_tbl)
register_s3_method("knitr", "knit_print", "sf",
function(x, ...) {
print(x, ..., n = 6)
}
)