forked from hadley/r-pkgs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.R
90 lines (76 loc) · 2.39 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
## seeded with content from same in adv-r
## deleted bits that seem irrelevant
## commented out bits that look like they may become relevant
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE
# cache = TRUE,
# fig.retina = 0.8, # figures are either vectors or 300 dpi diagrams
# dpi = 300,
# out.width = "70%",
# fig.align = 'center',
# fig.width = 6,
# fig.asp = 0.618, # 1 / phi
# fig.show = "hold"
)
options(
rlang_trace_top_env = rlang::current_env(),
rlang__backtrace_on_error = "none"
)
options(
digits = 3,
str = strOptions(strict.width = "cut")
)
if (knitr::is_latex_output()) {
knitr::opts_chunk$set(width = 69)
options(width = 69)
options(crayon.enabled = FALSE)
options(cli.unicode = TRUE)
}
# knitr::knit_hooks$set(
# small_mar = function(before, options, envir) {
# if (before) {
# par(mar = c(4.1, 4.1, 0.5, 0.5))
# }
# }
# )
# Make error messages closer to base R
registerS3method("wrap", "error", envir = asNamespace("knitr"),
function(x, options) {
msg <- conditionMessage(x)
call <- conditionCall(x)
if (is.null(call)) {
msg <- paste0("Error: ", msg)
} else {
msg <- paste0("Error in ", deparse(call)[[1]], ": ", msg)
}
msg <- error_wrap(msg)
knitr:::msg_wrap(msg, "error", options)
}
)
error_wrap <- function(x, width = getOption("width")) {
lines <- strsplit(x, "\n", fixed = TRUE)[[1]]
paste(strwrap(lines, width = width), collapse = "\n")
}
knitr::knit_hooks$set(chunk_envvar = function(before, options, envir) {
envvar <- options$chunk_envvar
if (before && !is.null(envvar)) {
old_envvar <<- Sys.getenv(names(envvar), names = TRUE, unset = NA)
do.call("Sys.setenv", as.list(envvar))
#print(str(options))
} else {
do.call("Sys.setenv", as.list(old_envvar))
}
})
check_quietly <- purrr::quietly(devtools::check)
install_quietly <- purrr::quietly(devtools::install)
shhh_check <- function(..., quiet = TRUE) {
out <- check_quietly(..., quiet = quiet)
out$result
}
pretty_install <- function(...) {
out <- install_quietly(...)
output <- strsplit(out$output, split = "\n")[[1]]
output <- grep("^(\\s*|[-|])$", output, value = TRUE, invert = TRUE)
c(output, out$messages)
}