I've created a custom chunk engine for knitr with knitr::knit_engines$set()
that takes some YAML specifications, interprets them, and creates a plot as output. If the knitr document is rendered to HTML I have it create shiny output which I convert to HTML with htmltools::knit_print.shiny.tag()
and that works fine. However, if I render to pdf/docx/odf I create a static plot object instead of a shiny output. That bit is fine, but all I can get it to render is a text representation of the plot (ie. a list object), rather than an image of the plot.
I need to somehow pass an image (I assume) to the output of knitr::knit_engines$set(), but if I try saving the plot to a png and then passing that as output, I just get the text representation of the file path instead.
knitr::knit_engines$set(example = function(options) {
#### read options$code and do stuff - works fine
p <- ggplot(data = mpg) + geom_jitter(aes(x = cty, y = hwy))
##### output results
# Shiny - outputs html text. Works fine
if(knitr::opts_knit$get("rmarkdown.pandoc.to") == "html"){
shiny::plotOutput(p) %>%
htmltools::knit_print.shiny.tag()
# Static output formats (pdf, etc)
} else {
#### attempt 1 ####
p # returns list object
#### attempt 2 ####
ggsave(filename = here("temp", "out.png"), plot = p)
knitr::include_graphics(here("temp", "out.png")) # Returns here("temp", "out.png")
}
})
I've looked through the knitr/R/engine.R on Github for inspiration, but have just gotten lost instead.
p # returns list object
withprint(p) # returns list object
?