如何在新引擎中包含地块 [英] How to include plots in a new engine
问题描述
我正在为knitr编写一个新引擎.该引擎可以根据输入生成作为ggplot对象(列表)或存储在文件(.png)中的图.我正在尝试将图像输出到knitr生成的html(pdf,md)文件中,但找不到方法.
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.
我尝试过:
-
include_graphics()
以及文件路径 - 在
engine_output()
函数中返回ggplot对象 -
knit_print()
有很多选择
include_graphics()
with the path of the file- return the ggplot object in
engine_output()
function knit_print()
with many options
什么都行不通!
这是引擎的代码.它是Ruby on GraalVM(Galaaz)的引擎.调用GalaazUtil.exec_ruby
将在knitr的同一R进程上执行Ruby代码,并在out
中返回执行的输出.
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.
生成图时,没有输出... knitr 如何识别在R块中生成了图?
When a plot in generated, there is no output... how does knitr identify that a plot was generated in the R chunk?
现在假设我可以访问文件中生成的图像,那么如何在knitr html页面中显示该图像?
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)
}
谢谢!
推荐答案
请参阅帮助页面?knitr::engine_output
上的详细信息"部分.您可以使用knitr::include_graphics()
.这是一个玩具示例(包括R徽标):
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.
```
这篇关于如何在新引擎中包含地块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!