繁体   English   中英

knitr(R) - 如何在HTML文件中嵌入图像?

[英]knitr (R) - how not to embed images in the HTML file?

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

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

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

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

我的.R文件从这里生成HTML:

library(knitr)

knit2html('knit.Rmd')

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

如果你看一下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"

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

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

虽然没经过测试......

您只需向.Rmd标头中的输出选项添加self_contained: no即可。 例如:

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

它不是knitr器, knitr只是在运行R块之后生成一个修改过的markdown文件。 所以你需要看一下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

这是一个在单独的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将图片保存到'无效'文件夹中。 任务不需要其余选项。

点击此按钮:

单击此按钮

确保未选中该复选框:

确保未选中该复选框

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM