【问题标题】:How to include plots in a new engine如何在新引擎中包含绘图
【发布时间】:2018-10-31 18:57:09
【问题描述】:

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

我试过了:

  • include_graphics() 带文件路径
  • engine_output()函数中返回ggplot对象
  • knit_print() 有很多选择

没有任何作用!

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

生成图时,没有输出... 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)
}

谢谢!

【问题讨论】:

    标签: r knitr


    【解决方案1】:

    请参阅帮助页面?knitr::engine_output 上的“详细信息”部分。您可以使用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.
    ```
    

    【讨论】:

    • 看起来这只在文档被编织时输出一些东西,有没有办法在rmarkdown文档中运行块时也显示图像?
    • @retorquere 不幸的是,没有。
    猜你喜欢
    • 2015-02-27
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    相关资源
    最近更新 更多