Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding figures and tables after bibliography in Rmarkdown

I want to add tables and figures after the bibliography in an R Markdown document. However, R Markdown documents will by default always add the bibliography to the end of the report.

Is there an easy way to add content to the document after the references?

Attempted Solutions

A previous answer showed that there is a way to put the appendix after the bibliography in R Markdown. This appendix is a separate file and is added to the document with after_body in the YAML header. I tried 2 different possible solutions and none of them worked.

  1. I tried to put my appendix in a different file, but I ran into the problem of losing my references in the main file, as all the appendices are cross-referenced in the body of the paper. All references turn to ?? once I put them in a different file.
  2. I put all my figures and tables in a different file while also keeping them in the main file. Then, I used results = "hide" to hide them in the main file. The idea was to create 2 separate PDFs and to merge them. Unfortunately, when the figures are hidden, the references also turn to ??.

Additional information

  • I am using the output format bookdown:pdf_document2
  • My figures are created by an .R file and imported into my R Markdown file with include_graphics().
like image 957
SavedByJESUS Avatar asked Jul 14 '18 04:07

SavedByJESUS


People also ask

How do I insert a table in Rmarkdown?

Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd or R document and select “Addins –> Insert Table”.

How do I add references in Rmarkdown?

The usual way to include citations in an R Markdown document is to put references in a plain text file with the extension . bib, in BibTex format. Then reference the path to this file in index.

How do I embed a plot in R markdown?

You can embed an R code chunk like this: ```{r} summary(cars) ``` You can also embed plots, for example: ```{r, echo=FALSE} plot(cars) ``` Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.


1 Answers

Rather than trying to include things in the after_body, you are better off just repositioning where the bibliography appears in the document. As explained in this answer, you can choose where the bibliography appears using <div id="refs"></div>.

Secondly, you can use bookdown to easily add appendices within a document. Using the header # (APPENDIX) Appendix {-} will change all following secton numbers from numbers to letters. Check out the bookdown book

Used in a full example:

---
title: "Untitled"
output: bookdown::pdf_document2
references:
- id: fenner2012a
  title: One-click science marketing
  author:
  - family: Fenner
    given: Martin
  container-title: Nature Materials
  volume: 11
  URL: 'http://dx.doi.org/10.1038/nmat3283'
  DOI: 10.1038/nmat3283
  issue: 4
  publisher: Nature Publishing Group
  page: 261-263
  type: article-journal
  issued:
    year: 2012
    month: 3
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(magrittr)
```

# Introduction

Here is a citation [@fenner2012a]

# References {-}

<div id="refs"></div>

\newpage 

# (APPENDIX) Appendix {-}

# Appendix A

Here is your table

```{r table, fig.pos="H"}
knitr::kable(mtcars[1:5, 1:5], caption = "A table") %>%
  kableExtra::kable_styling(latex_options = "HOLD_position")
```

enter image description here

Note: this will only work if you use pandoc's built-in citation package and won't work if you set citation_package: natbib in the YAML

like image 194
Michael Harper Avatar answered Oct 16 '22 14:10

Michael Harper