How to include plots in a new engine

老子叫甜甜 提交于 2021-02-10 14:42:00

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!