简体   繁体   English

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

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

This is probably very easy but I can't seem to find it in docs. 这可能很容易,但我似乎无法在文档中找到它。 I would like to not embed the generated images in the HTML file itself. 我想不将生成的图像嵌入HTML文件本身。

So basically I want knit2html() to produce a HTML file with seperate image files (which are then linked to / shown in the HTML). 所以基本上我希望knit2html()生成一个带有单独图像文件的HTML文件(然后链接到/显示在HTML中)。 The basic behaviour is that the script embeds the images as a base64 string. 基本行为是脚本将图像嵌入为base64字符串。 The problem with this is that in IE, large images won't show up (ie appear to be missing). 这个问题是在IE中,大图像不会显示(即看起来丢失)。 Any idea how I can seperate the images from the HTML output? 知道如何从HTML输出中分离图像吗?

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

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

And my .R file to generate the HTML from this: 我的.R文件从这里生成HTML:

library(knitr)

knit2html('knit.Rmd')

This example generates a HTML with the plot as an embedded base64 string. 此示例生成一个HTML,其中绘图为嵌入式base64字符串。

If you look at the knit2html help page, you will see that : 如果你看一下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.

Then you look at the markdownToHTML help page and read that there is the following argument : 然后,您查看markdownToHTML帮助页面,并读取有以下参数:

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

So you look at the markdownHTMLOptions (still not lost ?) and see the following option : 那你看看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.

With the following command, you should see the default options on your system : 使用以下命令,您应该看到系统上的默认选项:

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

So may be you can try to knit your markdown file with : 因此,您可以尝试使用以下方法编写markdown文件:

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

Not tested, though... 虽然没经过测试......

You can just add self_contained: no to the output options in the .Rmd header. 您只需向.Rmd标头中的输出选项添加self_contained: no即可。 For example: 例如:

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

Its not knitr that does this, knitr just produces a modified markdown file after running the R chunks. 它不是knitr器, knitr只是在运行R块之后生成一个修改过的markdown文件。 So you need to look at the help for the markdown package to figure out... 所以你需要看一下markdown包的帮助来弄清楚......

Its the base64_images option. 它是base64_images选项。 Coffee hasn't kicked in yet, so I haven't exactly sussed out how to set/reset individual markdown options, but clearing them all out works for me: 咖啡还没有踢,所以我还没有确切地说出如何设置/重置单个降价选项,但清除它们对我有用:

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

producing 生产

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

in foo.html . foo.html

If clearing all those options breaks other stuff, then read up on markdownHTMLOptions . 如果清除所有这些选项会破坏其他内容,那么请阅读markdownHTMLOptions

Here is a simple way to have figures in a separate html file, which will reduce its size significantly. 这是一个在单独的html文件中包含数字的简单方法,这将显着减小其大小。

Add this chunk in the beginning of the *.rmd file: 在* .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)
```

Option 'fig.path' tells R to save pictures into 'Figs' folder. 选项'fig.path'告诉R将图片保存到'无效'文件夹中。 The rest of options is not required for the task. 任务不需要其余选项。

Click this button: 点击此按钮:

单击此按钮

Make sure the check box is not checked: 确保未选中该复选框:

确保未选中该复选框

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

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