-
Notifications
You must be signed in to change notification settings - Fork 684
/
common.R
157 lines (127 loc) · 3.85 KB
/
common.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
set.seed(451)
library(ggplot2)
conflicted::conflict_prefer("Position", "ggplot2")
library(dplyr)
conflicted::conflict_prefer("filter", "dplyr")
conflicted::conflict_prefer("pull", "dplyr") # in case git2r is loaded
library(tidyr)
conflicted::conflict_prefer("extract", "tidyr")
options(digits = 3, dplyr.print_min = 6, dplyr.print_max = 6)
options(crayon.enabled = FALSE)
# suppress startup message
library(maps)
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
fig.show = "hold",
dpi = 300,
cache = TRUE
)
is_latex <- function() {
identical(knitr::opts_knit$get("rmarkdown.pandoc.to"), "latex")
}
status <- function(type) {
status <- switch(type,
polishing = "should be readable but is currently undergoing final polishing",
restructuring = "is undergoing heavy restructuring and may be confusing or incomplete",
drafting = "is currently a dumping ground for ideas, and we don't recommend reading it",
complete = "is largely complete and just needs final proof reading",
stop("Invalid `type`", call. = FALSE)
)
class <- switch(type,
polishing = "note",
restructuring = "important",
drafting = "important",
complete = "note"
)
callout <- paste0(
"\n",
"::: {.callout-", class, "} \n",
"You are reading the work-in-progress third edition of the ggplot2 book. ",
"This chapter ", status, ". \n",
"::: \n"
)
cat(callout)
}
# Draw parts of plots -----------------------------------------------------
draw_legends <- function(...) {
plots <- list(...)
gtables <- lapply(plots, function(x) ggplot_gtable(ggplot_build(x)))
guides <- lapply(gtables, gtable::gtable_filter, "guide-box")
one <- Reduce(function(x, y) cbind(x, y, size = "first"), guides)
grid::grid.newpage()
grid::grid.draw(one)
}
# Customised plot layout --------------------------------------------------
plot_hook_bookdown <- function(x, options) {
paste0(
begin_figure(x, options),
include_graphics(x, options),
end_figure(x, options)
)
}
begin_figure <- function(x, options) {
if (!knitr_first_plot(options))
return("")
paste0(
"\\begin{figure}[H]\n",
if (options$fig.align == "center") " \\centering\n"
)
}
end_figure <- function(x, options) {
if (!knitr_last_plot(options))
return("")
paste0(
if (!is.null(options$fig.cap)) {
paste0(
' \\caption{', options$fig.cap, '}\n',
' \\label{fig:', options$label, '}\n'
)
},
"\\end{figure}\n"
)
}
include_graphics <- function(x, options) {
opts <- c(
sprintf('width=%s', options$out.width),
sprintf('height=%s', options$out.height),
options$out.extra
)
if (length(opts) > 0) {
opts_str <- paste0("[", paste(opts, collapse = ", "), "]")
} else {
opts_str <- ""
}
paste0(" \\includegraphics",
opts_str,
"{", tools::file_path_sans_ext(x), "}",
if (options$fig.cur != options$fig.num) "%",
"\n"
)
}
knitr_first_plot <- function(x) {
x$fig.show != "hold" || x$fig.cur == 1L
}
knitr_last_plot <- function(x) {
x$fig.show != "hold" || x$fig.cur == x$fig.num
}
# control output lines ----------------------------------------------------
hook_output <- knitr::knit_hooks$get("output")
knitr::knit_hooks$set(output = function(x, options) {
lines <- options$output.lines
if (is.null(lines)) {
return(hook_output(x, options)) # pass to default hook
}
x <- unlist(strsplit(x, "\n"))
if (length(lines)==1) { # first n lines
if (length(x) > lines) {
# truncate the output, but add ....
x <- c(head(x, lines), more)
}
} else {
x <- x[lines] # don't add ... when we get vector input
}
# paste these lines together
x <- paste(c(x, ""), collapse = "\n")
hook_output(x, options)
})