Citations in DT:datatable
Asked Answered
C

1

6

I am working on a bookdown project where we have large tables that also contain citations. We are outputting in both html and pdf. The problem is that I cannot find a way to make the citations render in the tables.

For PDF output this latex workaround works: https://github.com/haozhu233/kableExtra/issues/214 However, for the html output I have been using DT:datatable and I cannot figure a way to make the references render.

Is there a way to get the markdown parsed citation in the table?

markdown example:

---
title: "Untitled"
output: html_document
bibliography: ["references.bib"]
---


In text citation [@chambers_2012].


A plan data.frame can render the reference if inserted as text that markdown can parse.

```{r, results='asis'}
library(dplyr)
library(DT)

data_frame(citation = "[@chambers_2012]")

```

But not in a DT.

```{r, results='asis'}
library(dplyr)
library(DT)

data_frame(citation = "[@chambers_2012]") %>% datatable()

```



# bibliography

Sample references.bib

@article{chambers_2012,
title = {A cross-platform toolkit for mass spectrometry and proteomics.},
author = {Chambers, Matthew C and Maclean, Brendan and Burke, Robert and Amodei, Dario and Ruderman, Daniel L and Neumann, Steffen and Gatto, Laurent and Fischer, Bernd and Pratt, Brian and Egertson, Jarrett and Hoff, Katherine and Kessner, Darren and Tasman, Natalie and Shulman, Nicholas and Frewen, Barbara and Baker, Tahmina A and Brusniak, Mi-Youn and Paulse, Christopher and Creasy, David and Flashner, Lisa and Kani, Kian and Moulding, Chris and Seymour, Sean L and Nuwaysir, Lydia M and Lefebvre, Brent and Kuhlmann, Frank and Roark, Joe and Rainer, Paape and Detlev, Suckau and Hemenway, Tina and Huhmer, Andreas and Langridge, James and Connolly, Brian and Chadick, Trey and Holly, Krisztina and Eckels, Josh and Deutsch, Eric W and Moritz, Robert L and Katz, Jonathan E and Agus, David B and {MacCoss}, Michael and Tabb, David L and Mallick, Parag},
pages = {918-920},
url = {http://www.nature.com/doifinder/10.1038/nbt.2377},
year = {2012},
month = {oct},
urldate = {2018-01-13},
journal = {Nature Biotechnology},
volume = {30},
number = {10},
issn = {1087-0156},
doi = {10.1038/nbt.2377},
pmid = {23051804},
pmcid = {PMC3471674},
f1000-projects = {shared citations}
}
Clipboard answered 1/1, 2020 at 21:8 Comment(0)
C
4

I suggest you render the citations using the RefManageR package.

```{r}
library("RefManageR")
bib <- ReadBib(file = "references.bib")

invisible(data_frame(citation = RefManageR::TextCite(bib = bib)))
```


```{r}
data_frame(citation = RefManageR::TextCite(bib = bib,
                                           "chambers_2012",
                                           .opts = list(max.names = 1))) %>%
  DT::datatable()
```

datatable example screenshot

Or with link:

data_frame(citation = RefManageR::TextCite(bib = bib,
                                           "chambers_2012",
                                           .opts = list(style = "html",
                                                        max.names = 1))) %>%
  DT::datatable(escape = FALSE)

Check ?BibOptions for more options on output format, link type, etc.

P.S. for whatever reason, the first time the function is called does not seem to fully take in consideration all options given, hence that invisible() call.

Calends answered 7/1, 2020 at 11:36 Comment(2)
Interesting. Thanks! I assume this would require is to also use R code for all the in text citations (we use numbered citations in reality)? That would be quite awkward. I also wonder (since I didn't try it yet) if this will work when we have separate Rmd files in the gitbook. I wonder if it can keep track of the numeration.Clipboard
alright, as it was not mentioned in the question, I did not consider issues with numbered citations, and I supposed that it wouldn't be such a big issue to have these references as code in DT datatables. Numeric citations are indeed supported, but yes, I suspect they would require to use inline code to function consistently. I believe Gitbook has options to basically process all chapters as if they were a single Rmd, so that shouldn't be an issue. For reference, see also the section "6. UsingRefManageR in Dynamic Document" arxiv.org/pdf/1403.2036v1.pdfCalends

© 2022 - 2024 — McMap. All rights reserved.