Consider this simple example
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()
The code above will output a pdf with two pages. How can I show these two pages on my rmarkdown
document?
Using
---
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")
```
will only show the first page of the pdf
! Where is the other chart? :(
Any ideas?
Thanks!
The usual way to compile an R Markdown document is to click the Knit button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K ( Cmd + Shift + K on macOS).
R Markdown is an extension of the markdown syntax. R Markdown files are plain text files that typically have the file extension . Rmd . They are written using an extension of markdown syntax that enables R code to be embedded in them in a way which can later be executed.
RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.
There are three basic components of an R Markdown document: the metadata, text, and code.
One can use pdfpages
to include multiple pages from a PDF file at once. However, these are included on separate pages. While it is possible to add page numbers, you cannot easily put these images into a figure
environment. Fortunately, \includegraphics
has an option to use individual pages from a PDF. Unfortunately, knitr::include_graphics
does not allow passing additional arguments to \includegraphics
.
Here both possibilities:
---
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}
One could also put these into a R chunk with cat()
and result = 'asis'
. However, the options for setting caption etc. are still not used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With