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

时间:2022-10-23 09:48:32

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). The basic behaviour is that the script embeds the images as a base64 string. The problem with this is that in IE, large images won't show up (i.e. appear to be missing). Any idea how I can seperate the images from the HTML output?

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

4 个解决方案

#1


16  

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...

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

#2


10  

Its not knitr that does this, knitr just produces a modified markdown file after running the R chunks. So you need to look at the help for the markdown package to figure out...

它不是编织器,knitr只是在运行R块后生成一个修改过的markdown文件。因此,您需要查看markdown包的帮助以找出...

Its the base64_images option. 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:

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

 > 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。

#3


4  

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. The rest of options is not required for the task.

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

Click this button:

点击此按钮:

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

Make sure the check box is not checked:

确保未选中该复选框:

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

#4


4  

You can just add self_contained: no to the output options in the .Rmd header. For example:

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

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

#1


16  

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...

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

#2


10  

Its not knitr that does this, knitr just produces a modified markdown file after running the R chunks. So you need to look at the help for the markdown package to figure out...

它不是编织器,knitr只是在运行R块后生成一个修改过的markdown文件。因此,您需要查看markdown包的帮助以找出...

Its the base64_images option. 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:

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

 > 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。

#3


4  

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. The rest of options is not required for the task.

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

Click this button:

点击此按钮:

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

Make sure the check box is not checked:

确保未选中该复选框:

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

#4


4  

You can just add self_contained: no to the output options in the .Rmd header. For example:

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

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