【问题标题】:how to add a (multipage) pdf to rmarkdown?如何将(多页)pdf添加到rmarkdown?
【发布时间】:2018-09-24 19:45:34
【问题描述】:

考虑这个简单的例子

library(dplyr)
library(ggplot2)
library(tidyr)

mydata <- data_frame(group = c('a', 'a', 'a', 'b', 'b', 'b'),
                     x = c(1,2,3,5,6,7),
                     y = c(3,5,6,4,3,2))

mydata2 <- mydata %>% group_by(group) %>% 
  nest() %>% 
  mutate(myplot = map(data, ~ggplot(data = .x, aes(x = x, y = x)) + geom_point()))

pdf("P://mychart.pdf")
print(mydata2$myplot)
dev.off()

上面的代码将输出一个有两页的pdf。如何在我的rmarkdown 文档中显示这两页?

使用

---
title: "crazy test"
output:
  pdf_document
---

```{r global_options, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.pos = 'h')
```


ttt

## this is a test!!

```{r label, out.width = "85%", fig.cap = "caption"}
knitr::include_graphics(path = "P://mychart.pdf")
```

只会显示pdf的首页!另一个图表在哪里? :(

有什么想法吗?

谢谢!

【问题讨论】:

  • 能否为每个图(编号)制作一个pdf,然后通过循环将它们添加到markdown文档中?
  • 否,但假设您从外部获得pdf。你只有pdf。你会怎么做?
  • 我无法重现您的示例(nest 来自什么包?)。 include_graphics 函数似乎不支持多页 pdf。尝试使用staplr 拆分您的pdf(可能在一个带有echo=FALSE 的块中),然后将生成的图表包含在include_graphics 循环中。
  • 等一下,让我添加包。对此感到抱歉
  • 你可以使用 LaTeX 包 pdfpages,c.f. stackoverflow.com/a/52482385/8416610.

标签: r r-markdown bookdown


【解决方案1】:

可以使用pdfpages 一次包含PDF 文件中的多个页面。但是,这些都包含在单独的页面上。虽然可以添加页码,但您不能轻松地将这些图像放入figure 环境中。幸运的是,\includegraphics 可以选择使用 PDF 中的单个页面。不幸的是,knitr::include_graphics 不允许将其他参数传递给\includegraphics

这两种可能性:

---
title: "crazy test"
output:
  pdf_document
header-includes:
  - \usepackage{pdfpages}
---

```{r global_options, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.pos = 'h')
```


```{r, include=FALSE}
library(dplyr)
library(ggplot2)
library(tidyr)
library(purrr)

mydata <- data_frame(group = c('a', 'a', 'a', 'b', 'b', 'b'),
                     x = c(1,2,3,5,6,7),
                     y = c(3,5,6,4,3,2))

mydata2 <- mydata %>% group_by(group) %>% 
  nest() %>% 
  mutate(myplot = map(data, ~ggplot(data = .x, aes(x = x, y = x)) + geom_point()))

pdf("mychart.pdf")
print(mydata2$myplot)
dev.off()
```


## this is a test!!

Only first page

```{r label, out.width = "85%", fig.cap = "caption"}
knitr::include_graphics(path = "mychart.pdf")
```

All pages but w/o caption and taking a full page

\includepdf[pages=-,nup=2,pagecommand={}]{mychart.pdf}

Alternative, using explicit LaTeX commands.

\begin{figure}
\includegraphics[page=1,width=0.5\linewidth]{mychart.pdf}
\includegraphics[page=2,width=0.5\linewidth]{mychart.pdf}
\caption{\label{fig:test} Test.}
\end{figure}

也可以将它们放入带有cat()result = 'asis' 的R 块中。但是,设置标题等选项仍然没有使用。

【讨论】:

  • 很酷。本质上,只要我导入包,我就可以在 markdown 中添加任何类型的疯狂乳胶命令
  • 最好 include_graphics 包含一个 page_num 参数,这样我们就可以选择要显示的页面
  • @ℕʘʘḆḽḘ 由于 pandoc 的 raw_tex 扩展,包括原始 LateX 是可能的。为 PDF 中的单个页面添加特定选项会很奇怪,因为它在命令的许多用例中没有意义。但是,更通用的“传递这些附加参数”可能是值得的。
  • 你好我其实不同意。在许多情况下,人们只想在论文、附录的一部分或单个图表中添加给定的表格。在这种情况下,只包括必要的页面。否则必须使用 acrobat 手动提取页面,然后使用命令。如果您有很多 pdf,这真的很麻烦... :) 再次感谢!
  • 不知道,但你是第三个报告这个的人,c.f. stackoverflow.com/questions/52480647/…stackoverflow.com/questions/52198354/…
【解决方案2】:

这是带有 staplr 的 Rmd 解决方案。请注意,您需要安装 pdftk 才能使 split_pdf 工作

---
title: "crazy test"
output:
  pdf_document
---

```{r global_options, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.pos = 'h')
```

## Split pdf

```{r}
staplr::split_pdf("mychart.pdf", output_directory = ".", prefix = "mychart_")
```

## Add pdfs

```{r label, out.width = "85%", fig.cap = c("caption 1", "caption 2"), echo = FALSE}
flist <- list.files()
mychart_files <- flist[grep("mychart_", flist)]
knitr::include_graphics(mychart_files)
```

另外,包含图形不能循环工作。但它接受多个路径,所以效果很好。

【讨论】:

  • 是的,这可能会有问题……尤其是在 Ubuntu Bionic (18.04) 上。 Ralf 的建议似乎是一个可能的解决方案,使用 pdflatex。
【解决方案3】:

knitr 有一个名为out.extra 的特定块选项,允许将选项传递给\includegraphics 命令。在knitr doc 中查看此选项。

这意味着它可以用于分页选项page。使用上面的例子,你可以这样做

---
title: "crazy test"
output:
  pdf_document:
    keep_tex: TRUE
---

```{r global_options, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.pos = 'h')
```


```{r, include=FALSE}
library(dplyr)
library(ggplot2)
library(tidyr)
library(purrr)

mydata <- tibble(group = c('a', 'a', 'a', 'b', 'b', 'b'),
                     x = c(1,2,3,5,6,7),
                     y = c(3,5,6,4,3,2))

mydata2 <- mydata %>% group_by(group) %>% 
  nest() %>% 
  mutate(myplot = map(data, ~ggplot(data = .x, aes(x = x, y = x)) + geom_point()))

pdf("mychart.pdf")
print(mydata2$myplot)
dev.off()
```

Only first page

```{r label, out.width = "85%", fig.cap = "caption"}
knitr::include_graphics(path = "mychart.pdf")
```

second page

```{r label2, out.width = "85%", fig.cap = "caption", out.extra="page=2"}
knitr::include_graphics(path = "mychart.pdf")
```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    相关资源
    最近更新 更多