【问题标题】:Pdf figures not shown in html files produced by Rmarkdown when open with Firefox or Chrome使用 Firefox 或 Chrome 打开时,Rmarkdown 生成的 html 文件中未显示 Pdf 数字
【发布时间】:2017-09-18 21:10:51
【问题描述】:

我想在 Rmarkdown 生成的 html 文件中插入一个 pdf 图形。这是我的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()  
```

这是我的会话信息:

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

然后使用 Rstudio 版本 1.0.136 中的knit 按钮,我得到一个test.html 文件。插入的图显示在 RStudio 查看器面板和 Safari 中。但它不会在 Chrome 或 Firefox 中显示。有没有人有同样的问题?我该如何解决?

谢谢。

【问题讨论】:

    标签: html r pdf knitr r-markdown


    【解决方案1】:

    事实证明,当块选项 out.width 存在时,这是一个 knitr 问题,而我只是在 Github 上 fixed it。你可以devtools::install_github('yihui/knitr')

    没有out.width,Pandoc 使用<embed> 标签写出PDF 图像![](foo.pdf),这在所有主流浏览器中都有效,但是当指定out.width 时,knitr 将使用HTML 语法 <img src="foo.pdf" /> 写出图像,它在 Chrome 或 Firefox 中不起作用。我解决此问题的方法是将标签更改为 <embed>,就像 Pandoc 所做的那样。

    【讨论】:

    • 你的rmarkdown::pandoc_version() 是什么?我正在使用 1.17.2,即 RStudio 随附的版本。如果您安装了更高版本的 Pandoc,您可能希望将其删除。
    • 它现在适用于 Chrome 和 Firefox。谢谢!然而, 框架是如此明显(而且丑陋)。
    • 有趣的是,Safari 可以显示作为图像插入的 pdf,而不是 Chrome 和 Firefox。将pdf转换为png可以解决框架问题,但仍然不理想。非常感谢。
    • 2020年有没有更好的办法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多