如何将(多页)pdf 添加到 rmarkdown?
how to add a (multipage) pdf to rmarkdown?
考虑这个简单的例子
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
的第一页!另一个图表在哪里? :(
有什么想法吗?
谢谢!
这是使用 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)
```
此外,包含图形在循环中不起作用。但它接受多条路径,所以效果很好。
可以使用 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 块中。但是设置caption等选项还是没有用到
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")
```
考虑这个简单的例子
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
的第一页!另一个图表在哪里? :(
有什么想法吗?
谢谢!
这是使用 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)
```
此外,包含图形在循环中不起作用。但它接受多条路径,所以效果很好。
可以使用 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 块中。但是设置caption等选项还是没有用到
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")
```