Daijiang Li
Daijiang Li

Reputation: 697

Pdf figures not shown in html files produced by Rmarkdown when open with Firefox or Chrome

I want to insert a pdf figure into the html file produced by Rmarkdown. Here is my test.Rmd:

---
title: "knit include_graphics"
author: "D Li"
date: "4/21/2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

```{r out.width=800}
knitr::include_graphics("/Users/dli/Dropbox/UFL/SFL/Doc_beta_div/ecoregions_all.pdf")
sessionInfo()  
```

Here is my session info:

## R version 3.3.2 (2016-10-31)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Sierra 10.12.4
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
##  [1] backports_1.0.5      magrittr_1.5         rprojroot_1.2       
##  [4] tools_3.3.2          htmltools_0.3.5      yaml_2.1.14         
##  [7] Rcpp_0.12.10         stringi_1.1.2        rmarkdown_1.4.0.9001
## [10] knitr_1.15.19        stringr_1.2.0        digest_0.6.12       
## [13] evaluate_0.10

Then use the knit button in Rstudio Version 1.0.136, I get a test.html file. The inserted figure show in the RStudio viewer panel and in Safari. But it won't show in Chrome or Firefox. Does any one have the same problem? How should I solve it?

Thanks.

Imgur

Upvotes: 4

Views: 2622

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30154

It turns out that this is a knitr issue when the chunk option out.width is present, and I just fixed it on Github. You may devtools::install_github('yihui/knitr').

Without out.width, Pandoc writes out the PDF image ![](foo.pdf) using the <embed> tag, which works in all major browsers, but when out.width is specified, knitr will use the HTML syntax <img src="foo.pdf" /> to write out the image, and it does not work in Chrome or Firefox. The way that I fixed this issue was to change the tag to <embed> like what Pandoc does.

Upvotes: 2

Related Questions