【问题标题】:knitr (R) - how not to embed images in the HTML file?knitr (R) - 如何不在 HTML 文件中嵌入图像?
【发布时间】:2018-11-09 15:11:30
【问题描述】:

这可能很容易,但我似乎无法在文档中找到它。我不想将生成的图像嵌入 HTML 文件本身。

所以基本上我希望 knit2html() 生成一个带有单独图像文件的 HTML 文件(然后链接到/显示在 HTML 中)。基本行为是脚本将图像嵌入为 base64 字符串。这样做的问题是在 IE 中,大图像不会显示(即似乎丢失了)。知道如何将图像与 HTML 输出分开吗?

我的示例 .Rmd 文件('knit.Rmd'):

```{r}
plot(3)
```

还有我的 .R 文件来生成 HTML:

library(knitr)

knit2html('knit.Rmd')

此示例生成一个 HTML,其中该图作为嵌入的 base64 字符串。

【问题讨论】:

  • 你能给我们一个简短的降价示例,说明你在做什么,并列出你正在调用的 R 函数...
  • 我添加了一个小例子。

标签: r markdown knitr


【解决方案1】:

如果您查看 knit2html 帮助页面,您会看到:

This is a convenience function to knit the input markdown source and
call ‘markdownToHTML()’ in the ‘markdown’ package to convert the
result to HTML.

然后您查看markdownToHTML 帮助页面并阅读到有以下参数:

 options: options that are passed to the renderer.  see
           ‘markdownHTMLOptions’.

所以您查看markdownHTMLOptions(仍然没有丢失?)并查看以下选项:

 ‘'base64_images'’ Any local images linked with the ‘'<img>'’ tag
      to the output HTML will automatically be converted to base64
      and included along with output.

使用以下命令,您应该会看到系统上的默认选项:

R> markdownHTMLOptions(default=TRUE)
[1] "use_xhtml"      "smartypants"    "base64_images"  "mathjax"       
[5] "highlight_code"

因此,您可以尝试使用以下方法编写您的降价文件:

knit2html("knit.Rmd", options=c("use_xhtml","smartypants","mathjax","highlight_code"))

虽然没有测试...

【讨论】:

    【解决方案2】:

    您可以将self_contained: no 添加到 .Rmd 标头的输出选项中。例如:

    ---
    title: "Data visualisation with ggplot"
    output:
      html_document:
        self_contained: no
        toc: yes
        toc_float: yes
    ---
    

    【讨论】:

      【解决方案3】:

      它不是 knitr 这样做的,knitr 只是在运行 R 块后生成一个修改后的降价文件。所以你需要查看markdown包的帮助来弄清楚...

      它是base64_images 选项。咖啡还没有开始,所以我还没有完全弄清楚如何设置/重置单个降价选项,但将它们全部清除对我来说很有效:

       > knit2html("foo.Rmd",options="")
      

      生产

       <p><img src="figure/unnamed-chunk-1.png" alt="plot of chunk unnamed-chunk-1"> </p>
      

      foo.html.

      如果清除所有这些选项会破坏其他内容,请继续阅读 markdownHTMLOptions

      【讨论】:

      • 首先,将 .Rmd 文件中的 R 块渲染成修改后的 knitr .md 文件。然后渲染成 .html,例如render('foo.Rmd')。 R 解释器显示通用 knitr 文件名。 r-bloggers.com/r-knitr-markdown-html
      【解决方案4】:

      这是一种将图形放在单独的 html 文件中的简单方法,这将显着减小其大小。

      在 *.rmd 文件的开头添加这个块:

      ```{r global_options, include=FALSE}
      #suppress the warnings and other messages from showing in the knitted file.
      knitr::opts_chunk$set(fig.width=8, fig.height=6, fig.path='Figs/',
                            echo=TRUE, warning=FALSE, message=FALSE)
      ```
      

      选项 'fig.path' 告诉 R 将图片保存到 'Figs' 文件夹中。该任务不需要其余选项。

      点击这个按钮:

      确保复选框未选中:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-20
        • 1970-01-01
        • 2012-06-16
        • 2019-04-30
        • 1970-01-01
        • 1970-01-01
        • 2010-12-23
        相关资源
        最近更新 更多