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

Bar*_*art 22 markdown r knitr

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

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

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

```{r}
plot(3)
```
Run Code Online (Sandbox Code Playgroud)

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

library(knitr)

knit2html('knit.Rmd')
Run Code Online (Sandbox Code Playgroud)

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

jub*_*uba 19

如果您查看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.
Run Code Online (Sandbox Code Playgroud)

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

 options: options that are passed to the renderer.  see
           ‘markdownHTMLOptions’.
Run Code Online (Sandbox Code Playgroud)

所以你看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.
Run Code Online (Sandbox Code Playgroud)

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

R> markdownHTMLOptions(default=TRUE)
[1] "use_xhtml"      "smartypants"    "base64_images"  "mathjax"       
[5] "highlight_code"
Run Code Online (Sandbox Code Playgroud)

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

knit2html("knit.Rmd", options=c("use_xhtml","smartypants","mathjax","highlight_code"))
Run Code Online (Sandbox Code Playgroud)

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


Lis*_*ine 11

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

---
title: "Data visualisation with ggplot"
output:
  html_document:
    self_contained: no
    toc: yes
    toc_float: yes
---
Run Code Online (Sandbox Code Playgroud)


Spa*_*man 10

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

它的base64_images选择.咖啡还没有踢,所以我还没有确切地说出如何设置/重置单个降价选项,但清除它们对我有用:

 > knit2html("foo.Rmd",options="")
Run Code Online (Sandbox Code Playgroud)

生产

 <p><img src="figure/unnamed-chunk-1.png" alt="plot of chunk unnamed-chunk-1"> </p>
Run Code Online (Sandbox Code Playgroud)

foo.html.

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


sbo*_*ond 5

这是在单独的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)
```
Run Code Online (Sandbox Code Playgroud)

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

点击此按钮:

点击这个按钮

确保未选中该复选框:

确保未选中该复选框