-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmd_functions.R
45 lines (40 loc) · 1.47 KB
/
rmd_functions.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
# rmarkdown helper functions
subchunkify <- function(g, i, fig_height = 12, fig_width = 10, caption = "''",
add_class = NULL, other_args = "") {
####### Function Description ########
# function to allow for multiple plots of different sizes and captions
# within a single R code chunk
#
# code adapted from http://michaeljw.com/blog/post/subchunkify/
#
# inputs:
# - g = plot
# - i = chunk id (should be unique for each plot)
# - fig_height = height of figure
# - fig_width = width of figure
# - caption = figure caption; should be within surrounded by two sets of
# quotes, e.g., "'This is a valid caption.'"
# - add_class = css classes to add to object
# - other_args = other arguments to pass to R code chunk header
#######
g_deparsed <- paste0(deparse(function() {g}), collapse = '')
if (!identical(other_args, "")) {
if (!startsWith(other_args, ",")) {
other_args <- paste0(", ", other_args)
}
}
sub_chunk <- paste0("
`","``{r subchunk_", i,
", fig.height=", fig_height, ", fig.width=", fig_width,
", fig.cap=", caption,", echo=FALSE", other_args, "}",
"\n(", g_deparsed, ")()",
"\n`","``
")
if (!is.null(add_class)) {
cat(sprintf("<div class='%s'>", paste(add_class)))
}
cat(knitr::knit(text = knitr::knit_expand(text = sub_chunk), quiet = TRUE))
if (!is.null(add_class)) {
cat("</div>")
}
}