I'm using RMarkdown to functionally create a document using results = 'asis'
with a purrr::map
. There are multiple plots that come out of the chunk on each purrr
iteration. Most of them are the same size, and can be set using the chunk options for figure size. However one or two need to have a different size. It is not possible to separate the code into different chunks due to the way the loop/map is set up.
The closest I've found is http://michaeljw.com/blog/post/subchunkify/, however when I use this on the plot that needs different sizing, it causes the first iteration's plots that were output using the print() function to be recycled in the subchunkify's plots location.
Is there a different, less hacky way to do this? Or is there something obvious in the subchunkify code that would be fixable?
Here is subchunkify()
:
subchunkify <- function(g, fig_height=7, fig_width=5) {
g_deparsed <- paste0(deparse(
function() {g}
), collapse = '')
sub_chunk <- paste0("
`","``{r sub_chunk_", floor(runif(1) * 10000), ", 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))
}