考虑这个简单的例子
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
的首页!其他图表在哪里? :(
有什么想法吗?
谢谢!
答案 0 :(得分:3)
这是带有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)
```
此外,包含图形不能循环使用。但是它接受多个路径,因此效果很好。
答案 1 :(得分:3)
一个人可以使用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块中。但是,仍未使用用于设置字幕等的选项。
答案 2 :(得分:0)
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")
```