How to include a multipage PDF in RMarkdown with dynamic paths? - r

I was able to figure out how to include a multi-page PDF document in my RMarkdown output using "\includepdf", but I need to be able to change the path to different PDFs based on conditions in the data. I'd like to be able to call the PDF file path based on an object I set in a chunk beforehand (e.g. "test"). Any ideas?
Thanks!
---
title: "Personnel Reports"
output:
pdf_document
header-includes:
- \usepackage{pdfpages}
---
This works:
\includepdf[pages=-,pagecommand={}]{Person1_report.pdf}
```{r global_options, include=FALSE}
test <- "Person2_report.pdf"
```'
This doesn't work:
\includepdf[pages=-,pagecommand={}]{test}

---
title: "Personnel Reports"
output:
pdf_document:
keep_tex: true
header-includes:
- \usepackage{pdfpages}
---
```{r global_options, include=FALSE}
test <- "example-image-duck.pdf"
```
\includepdf[pages=-,pagecommand={}]{`r test`}

Related

Unable to include multiple flextable in RMarkdown PDF minipage

I am trying to use the R {flextable} package to create a PDF. It doesn't like {multicol} (SO: Flextable seems to be incompatible with multicol LaTex package) due to longtables not being allowed in multicol. So I have instead used {minipage}.
When trying with a single flextable the document succesfully knits:
---
title: "Untitled"
date: "30/08/2021"
output:
pdf_document:
latex_engine: lualatex
geometry: margin=1.5cm
---
\begin{minipage}[t]{0.5\linewidth}
```{r iris}
flextable::flextable(iris[1:5, ])
```
\end{minipage}
\begin{minipage}[t]{0.5\linewidth}
Content on the right hand side
\end{minipage}
However, when adding in a second table, it doesn't convert correctly to the .tex file:
---
title: "Untitled"
date: "30/08/2021"
output:
pdf_document:
latex_engine: lualatex
geometry: margin=1.5cm
---
\begin{minipage}[t]{0.5\linewidth}
```{r iris}
flextable::flextable(iris[1:5, ])
```
\end{minipage}
\begin{minipage}[t]{0.5\linewidth}
```{r iris2}
flextable::flextable(iris[1:5, ])
```
\end{minipage}
The .tex content looks fine for the first minipage, but appears as this in the second minipage:
\textbackslash begin\{minipage\}{[}t{]}\{0.5\linewidth\}
Is there something I need to add to the Rmd file to prevent this from happening? I have tried using print and cat and causes the same output/error.
The problem seems to be that Pandoc does not understand that the second \begin{minipage}[t]{0.5\linewidth} is supposed to be verbatim LaTeX as well.
As a workaround, you can mark this line as raw LaTeX:
```{=latex}
\begin{minipage}[t]{0.5\linewidth}
```
The same applies to the closing \end{minipage}.
However, this generates a paragraph break between the two minipages such that they are not side-by-side anymore. The only remedy I found thus far is to use the raw LaTeX syntax for the first minipage, too:
---
title: "2 Flextables"
output:
pdf_document:
latex_engine: lualatex
keep_tex: yes
geometry: margin=1.5cm
---
```{=latex}
\begin{minipage}[t]{0.5\linewidth}
```
```{r iris}
flextable::flextable(iris[1:5, 2:4])
```
```{=latex}
\end{minipage}%
\begin{minipage}[t]{0.5\linewidth}
```
```{r iris2}
flextable::flextable(iris[1:5, 2:4])
```
```{=latex}
\end{minipage}
```
Output:

R Markdown : \#ref() not working

I'm having a lot of trouble getting basic references to work in R Markdown. To reduce complexity from my original project, I've decided to use the bookdown example code, but I'm experiencing the same problem. Here's a link to the intro exmample code: https://github.com/rstudio/bookdown-demo/blob/master/01-intro.Rmd
When I use Knitr to HTML or PDF the file is being generated fine but the references are not working, instead the file will just containt "#ref(example)". Here is an image to show better the output (my emphasis added in red):
Direct link to image: https://i.imgur.com/2yxB5h3.png
Here is a minimal example:
---
title: "Minimal"
output:
pdf_document:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \#ref(fig:minGraph)
```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```
With the output appearing as such:
https://i.imgur.com/J3UECqn.png
If you want make use of the bookdown extensions in a normal rmarkdown document you can use bookdown::html_document2 and bookdown::pdf_document2 instead of rmarkdown::html_document and rmarkdown::pdf_document. Example:
---
title: "Minimal"
output:
bookdown::html_document2:
fig_caption: yes
bookdown::pdf_document2:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \#ref(fig:minGraph)
```{r minGraph, echo=FALSE, fig.cap="test"}
plot(x=1)
```
Looks like I was getting my syntax confused by reading the bookdown guide while using just R markdown. Thanks to Ralf for pointing me in the this direction. The correct minimal code would be like so:
---
title: "Minimal"
output:
pdf_document:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \ref{fig:minGraph}
```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```

How to suppress automatic figure numbering in Rmarkdown / pandoc

I have the following Rmarkdown (.Rmd) document where I call existing .png images and create a .pdf with captions. By default, pandoc? is automatically adding "Figure #." before the caption for each picture. I can see how this would be the normal thing to do, but in my case I would like to define this. I have found variations on this topic but don't seem to find a solution. Below is an example of how my .Rmd file looks:
---
title: "TITLE"
author: "ME"
date: "`r Sys.Date()`"
output:
pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
![Caption for figure 1](figures/plot1.png)
\newpage
![Caption for figure 2](figures/plot2.png)
You could use the caption-package
Create a .tex-file that you specify the following in, this below remove the entire label and you are free to hardcode the labels.
\usepackage{caption}
\captionsetup[figure]{labelformat=empty}
Then your .rmd should look like this:
---
title: "TITLE"
author: "ME"
date: "`r Sys.Date()`"
output:
pdf_document:
includes:
in_header: YourName.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
![Caption for figure 1](figures/plot1.png)
\newpage
![Caption for figure 2](figures/plot2.png)
Simplified: As suggested in the comments, we can achieve this within our .Rmd file, as shown below.
---
title: "TITLE"
author: "ME"
date: "`r Sys.Date()`"
output:
pdf_document:
header-includes:
- \usepackage{caption}
- \captionsetup[figure]{labelformat=empty}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
![Caption for figure 1](figures/plot1.png)
\newpage
![Caption for figure 2](figures/plot2.png)

In Rmd documents rendered to pdf, preventing extra line breaks in R chunks?

The following code results in an extra line break between rnorm(1) and the return value. How do I remove this extra line?
---
title: "Lecture2"
output: pdf_document
---
```{r}
rnorm(1)
```
I have miktex installed and am using knitr.
Maybe you should evaluate as inline code (i.e. `r norm(1)`).
This approach works only when highlight=FALSE and has the unfortunate side effect of removing the ability to create line breaks with new paragraphs...
---
title: "Untitled"
output:
beamer_presentation:
keep_tex: true
includes:
in_header: header.tex
---
```{r, highlight=FALSE}
rnorm(1)
```
in header.tex
%code from http://tex.stackexchange.com/questions/43331/control-vertical-space-before-and-after-verbatim-environment
\usepackage{etoolbox}
\makeatletter
\preto{\#verbatim}{\topsep=0pt \partopsep=0pt }
\makeatother
%https://www.sharelatex.com/learn/Paragraph_formatting
\setlength{\parskip}{0 em}

add latex to rmarkdown to wrap long lines using listings package

Long urls do not break when knitting to PDF in RStudio. This can be solved with the listings package if using .Rnw, but I'm not sure what can be done if using RMarkdown.
It seems like it's possible to include some LaTeX commands, but I have not figured out a way to implement the listings solution in .Rmd.
I tried:
---
title: "my title"
header-includes:
- \usepackage{listings}
output:
pdf_document
---
render_listings()
```{r setup, include=FALSE}
library(knitr)
render_listings()
```
```{r url, include=TRUE}
url <- "https://raw.githubusercontent.com/yihui/knitr-examples/master/066-listings-breaklines.Rnw"
```

Resources