问题
I'm writing a new engine for knitr. This engine can, depending on the input, generate a plot as either a ggplot object (list) or stored in a file (.png). I'm trying to output the image to the html (pdf, md) file that is generated by knitr but cannot find how.
I've tried:
include_graphics()
with the path of the file- return the ggplot object in
engine_output()
function knit_print()
with many options
nothing works!
Here is the code of the engine. It's an engine for Ruby on GraalVM (Galaaz). Calling GalaazUtil.exec_ruby
will execute the Ruby code on the same R process of knitr and return in out
the output of the execution.
When a plot in generated, there is no output... how does knitr identify that a plot was generated in the R chunk?
Now assuming that I have access to the generated image in a file, how to I make this show in my knitr html page?
eng_ruby = function(options) {
block_code = paste(options$code, collapse = "\\n");
code = paste0("GalaazUtil.exec_ruby(",
shQuote(block_code),
")
");
out = eval.polyglot("ruby", code);
engine_output(options, block_code, out)
}
Thanks!
回答1:
Please see the "Details" section on the help page ?knitr::engine_output
. You can use knitr::include_graphics()
. Here is a toy example (include the R logo):
```{r}
knitr::knit_engines$set(Rlogo = function(options) {
path = 'logo.jpg'
file.copy(file.path(R.home('doc'), 'html', 'logo.jpg'), path)
knitr::engine_output(options, out = list(knitr::include_graphics(path)))
})
```
```{Rlogo}
Whatever.
```
来源:https://stackoverflow.com/questions/53090259/how-to-include-plots-in-a-new-engine