-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRmd_functions
More file actions
59 lines (50 loc) · 1.85 KB
/
Copy pathRmd_functions
File metadata and controls
59 lines (50 loc) · 1.85 KB
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
# Subchunkify
# Dynamically create chunks, which fixes the interactive plots
# not rendered in loops or functions
subchunkify <- function(g, chunkname, fig_height=6, fig_width=6) {
g_deparsed <- paste0(deparse(function() {g}), collapse = '')
sub_chunk <- paste0("`","``{r subchunk_", chunkname, ", fig.height=", fig_height,
", fig.width=", fig_width, " , echo=FALSE}",
"\n(", g_deparsed, ")()", "\n`", "``")
cat(knitr::knit(text = knitr::knit_expand(text = sub_chunk), quiet = TRUE))
}
# Linkify
# Convert a file path to a link when Rmd in rendered
linkify <- function(filepath) {
cat(paste0('\n\n[', filepath, '](', filepath, ')\n\n'))
}
# Create log file with path to running R (and thus giving clues as to
# which conda environment was used if applicable), rmarkdown::render
# parameters, messages, warnings and errors.
# Add this chunk at the begining of the Rmd
```{r global_options, include=FALSE}
# sets up global options for rendering RMarkdown into HTML.
knitr::opts_chunk$set(
warning=TRUE,
message=TRUE,
error=TRUE # technically this means keep running after an error, but will now be caught by knitr::knit_hooks$set(error = ...)
)
# log file
log_fn <- paste0(knitr::current_input(), '.log')
# sets up the redirect of messages to log file
knitr::knit_hooks$set(
message = function(x, options) {
cat(x, file = log_fn, append = TRUE)
return()
},
warning = function(x, options) {
cat(x, file = log_fn, append = TRUE)
return()
},
error = function(x, options) {
cat(x, file = log_fn, append = TRUE)
return(stop(x))
}
)
# append current run metadata
message(paste0('\n', Sys.time()))
message(paste0('Environment: ', Sys.which("R")))
if (exists("params") & !exists("params", mode='function')) {
message(paste0('rmarkdown::render params: ', paste0(params, collapse=';')))
}
```