如何在rmarkdown中添加(多页)pdf?

ℕʘʘ*_*ḆḽḘ 5 r r-markdown bookdown

考虑这个简单的例子

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()
Run Code Online (Sandbox Code Playgroud)

上面的代码将输出包含两页的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")
```
Run Code Online (Sandbox Code Playgroud)

只会显示pdf!的首页!其他图表在哪里?:(

在此处输入图片说明

有任何想法吗?

谢谢!

Ral*_*ner 8

可以使用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}
Run Code Online (Sandbox Code Playgroud)

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