Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add a (multipage) pdf to rmarkdown?

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? :(

enter image description here

Any ideas?

Thanks!

like image 865
ℕʘʘḆḽḘ Avatar asked Sep 24 '18 19:09

ℕʘʘḆḽḘ


People also ask

How do I compile a PDF in R Markdown?

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).

What is the difference between markdown and R Markdown?

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.

What is knitr R Markdown?

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.

What are the three types of sections in an R Markdown document?

There are three basic components of an R Markdown document: the metadata, text, and code.


1 Answers

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.

like image 117
Ralf Stubner Avatar answered Nov 02 '22 06:11

Ralf Stubner