如何在新引擎中包含绘图

How to include plots in a new engine

我正在为 knitr 编写一个新引擎。根据输入,该引擎可以将绘图生成为 ggplot 对象(列表)或存储在文件 (.png) 中。我正在尝试将图像输出到 knitr 生成的 html (pdf, md) 文件,但找不到方法。

我试过:

没有任何效果!

这是引擎的代码。它是 GraalVM (Galaaz) 上 Ruby 的引擎。调用 GalaazUtil.exec_ruby 将在 knitr 的同一 R 进程上执行 Ruby 代码,并在 out 执行输出中执行 return 代码。

生成绘图时,没有输出...knitr如何识别在 R 块中生成绘图?

现在假设我可以访问文件中生成的图像,如何在我的 knitr html 页面中显示此内容?

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)
}

谢谢!

请参阅帮助页面 ?knitr::engine_output 上的 "Details" 部分。您可以使用 knitr::include_graphics()。这是一个玩具示例(包括 R 徽标):

```{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.
```