Knitr (R) - How Not to Embed Images in the HTML File

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

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

How can I knit a Rmarkdown file into a html file which embeds plots?

What is the equivalent commands to RStudio's "knit to html" button?

Submit your output in a YAML header and then use

rmarkdown::render("Your-RMD-file.Rmd")

Or see the documentation for the render function and their arguments.

Include images for pdf and html format

Short answer: your image is a PDF File


It always helps to include a minimal, reproducible example. As the path to your image was not provided, I have created one here which creates a local file cars.pdf:

---
title: Images in Knitr
output:
html_document: default
pdf_document: default
---

```{r}
# Create an example plot
pdf("cars.pdf")
plot(cars)
dev.off()

```

```{r}
knitr::include_graphics("cars.pdf")
```

Comparing the HTML and PDF, we can see the behaviour you report, whereby the images aren't shown in the HTML:

Sample Image

The problem stems from the image being saved as a PDF. From my understanding, HTML has no native way to display such images. It therefore doesn't display the image within the output.

As a workaround, you would be better to save the graphic as a PNG or JPEG.

Embedding scalable images in R Markdown HTML output file

You may want to use out.width instead of fig.width and fig.height, with percentage, which will be percentage of the text area. You can use it with include_graphics(). If you do not set out.height, the ratio will stay ok.

```{r, echo=FALSE, out.width='80%'}
knitr::include_graphics("images/image.png")
```


Related Topics



Leave a reply



Submit