forked from hadley/adv-r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.R
68 lines (58 loc) · 1.81 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
library(methods)
set.seed(1014)
options(digits = 3)
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
cache = TRUE,
out.width = "70%",
fig.align = 'center',
fig.width = 6,
fig.asp = 0.618, # 1 / phi
fig.show = "hold"
)
# options("crayon.enabled" = TRUE, "crayon.colors" = 256L)
# crayon::num_colors(TRUE)
#
# colourise_chunk <- function(x, options) {
# fence = paste(rep("`", 3), collapse = '')
#
# x = gsub(paste0('[\n]{2,}(', fence, '| )'), '\n\n\\1', x)
# x = gsub('[\n]+$', '', x)
# x = gsub('^[\n]+', '\n', x)
# if (isTRUE(options$collapse)) {
# x = gsub(paste0('\n([', fence_char, ']{3,})\n+\\1(', tolower(options$engine), ')?\n'), "\n", x)
# }
# x <- ansistrings::ansi_to_html(x, fullpage = FALSE)
#
# if (is.null(s <- options$indent)) return(x)
# line_prompt(x, prompt = s, continue = s)
# }
knitr::knit_hooks$set(
small_mar = function(before, options, envir) {
if (before) {
par(mar = c(4.1, 4.1, 0.5, 0.5))
}
}
# chunk = colourise_chunk
)
# Previously in oldbookdown -----------------------------------------------
doc_type <- function() knitr::opts_knit$get('rmarkdown.pandoc.to')
begin_sidebar <- function(title = NULL) {
if (identical(doc_type(), "latex")) {
# Suppress sidebars for now - pandoc doesn't convert markdown inside
# a latex environment, so this technique required post-processing,
# and I don't want to bother with that until I start building the
# final block
# knitr::asis_output(paste0("\\begin{SIDEBAR}", title, "\\end{SIDEBAR}\n"))
} else {
knitr::asis_output(paste0("<div class = 'sidebar'><h3>", title, "</h3>\n\n"))
}
}
end_sidebar <- function() {
if (identical(doc_type(), "latex")) {
# knitr::asis_output("\\begin{ENDSIDEBAR}\\end{ENDSIDEBAR}\n")
} else {
knitr::asis_output("</div>\n")
}
}