Bart
Bart

Reputation: 759

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.

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?

My example .Rmd file ('knit.Rmd'):

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

And my .R file to generate the HTML from this:

library(knitr)

knit2html('knit.Rmd')

This example generates a HTML with the plot as an embedded base64 string.

Upvotes: 26

Views: 11026

Answers (4)

Lisa DeBruine
Lisa DeBruine

Reputation: 868

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

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

Upvotes: 19

sbond
sbond

Reputation: 178

Here is a simple way to have figures in a separate html file, which will reduce its size significantly.

Add this chunk in the beginning of the *.rmd file:

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

Click this button:

Click this button

Make sure the check box is not checked:

Make sure the check box is not checked

Upvotes: 6

juba
juba

Reputation: 49033

If you look at the knit2html help page, you will see that :

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 :

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

So you look at the markdownHTMLOptions (still not lost ?) and see the following option :

 ‘'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 :

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

Not tested, though...

Upvotes: 19

Spacedman
Spacedman

Reputation: 94172

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

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:

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

producing

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

in foo.html.

If clearing all those options breaks other stuff, then read up on markdownHTMLOptions.

Upvotes: 10

Related Questions