29

I'm trying to use knitcitations and add bibliography to the R Markdown document that I'm drafting in R Studio. The header of my document looks like this:

---
title: "Some Title"
author: "Me"
date: "September 2015"
bibliography: bibliography.bib
output:
  pdf_document: 
    highlight: tango
    number_sections: yes
    toc: yes
---

I want to add the bibliography at the end using the following code:

```{r generateBibliography, echo=FALSE, eval=TRUE, message=FALSE, warning=FALSE}
require("knitcitations")
cleanbib()
options("citation_format" = "pandoc")
read.bibtex(file = "bibliography.bib")
```

The reference file bibliography.bib has the following content:

@article{debarsy_testing_2010,
    title = {Testing for spatial autocorrelation in a fixed effects panel data model},
    volume = {40},
    issn = {0166-0462},
    url = {http://www.sciencedirect.com/science/article/pii/S0166046210000451},
    doi = {10.1016/j.regsciurbeco.2010.06.001},
    abstract = {The aim of this paper is to assess the relevance of spatial autocorrelation in a fixed effects panel data model and in the affirmative, to identify the most appropriate spatial specification as this appears to be a crucial point from the modeling perspective of interactive heterogeneity. Several {LM} test statistics as well as their {LR} counterparts, which allow discriminating between endogenous spatial lag versus spatially autocorrelated errors, are therefore proposed. Monte Carlo experiments show their good finite sample performance. Finally, an empirical application is provided in the framework of the well-known Feldstein–Horioka puzzle.},
    pages = {453--470},
    number = {6},
    journaltitle = {Regional Science and Urban Economics},
    shortjournal = {Regional Science and Urban Economics},
    author = {Debarsy, Nicolas and Ertur, Cem},
    urldate = {2015-10-01},
    date = {2010-11},
    keywords = {Panel data, Spatial autocorrelation, Test statistics},
    file = {complex_zotero_path}
}

@article{lamichhane_spatial-temporal_2015,
    title = {Spatial-Temporal Modeling of Neighborhood Sociodemographic Characteristics and Food Stores},
    volume = {181},
    issn = {0002-9262, 1476-6256},
    url = {http://aje.oxfordjournals.org/content/181/2/137},
    doi = {10.1093/aje/kwu250},
    abstract = {The literature on food stores, neighborhood poverty, and race/ethnicity is mixed and lacks methods of accounting for complex spatial and temporal clustering of food resources. We used quarterly data on supermarket and convenience store locations from Nielsen {TDLinx} (Nielsen Holdings N.V., New York, New York) spanning 7 years (2006–2012) and census tract-based neighborhood sociodemographic data from the American Community Survey (2006–2010) to assess associations between neighborhood sociodemographic characteristics and food store distributions in the Metropolitan Statistical Areas ({MSAs}) of 4 {US} cities (Birmingham, Alabama; Chicago, Illinois; Minneapolis, Minnesota; and San Francisco, California). We fitted a space-time Poisson regression model that accounted for the complex spatial-temporal correlation structure of store locations by introducing space-time random effects in an intrinsic conditionally autoregressive model within a Bayesian framework. After accounting for census tract–level area, population, their interaction, and spatial and temporal variability, census tract poverty was significantly and positively associated with increasing expected numbers of supermarkets among tracts in all 4 {MSAs}. A similar positive association was observed for convenience stores in Birmingham, Minneapolis, and San Francisco; in Chicago, a positive association was observed only for predominantly white and predominantly black tracts. Our findings suggest a positive association between greater numbers of food stores and higher neighborhood poverty, with implications for policy approaches related to food store access by neighborhood poverty.},
    pages = {137--150},
    number = {2},
    journaltitle = {American Journal of Epidemiology},
    shortjournal = {Am. J. Epidemiol.},
    author = {Lamichhane, Archana P. and Warren, Joshua L. and Peterson, Marc and Rummo, Pasquale and Gordon-Larsen, Penny},
    urldate = {2015-10-01},
    date = {2015-01-15},
    langid = {english},
    pmid = {25515169},
    keywords = {food availability, food stores, intrinsic conditionally autoregressive model, neighborhood characteristics, Poverty, sociodemographic factors, spatial-temporal modeling, supermarkets},
    file = {complex_zotero_path}
}

However, the produced output appears as comments, not as bibliographic entry: sample entries

The file is compiled with this code:

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS _paper.md --to latex 
--from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures 
--output _paper.pdf --filter pandoc-citeproc --table-of-contents --toc-depth 2 --template "path_\latex\default.tex" 
--number-sections --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in" --bibliography bibliography.bib 

For brevity, I changed the paths to _paper and _paper.


When I tried to follow the advice on including bibliography in RStudio the document was produced with no bibliographic entries at all. Hence my question, where I'm making the mistake and how can I force generation of bibliographic entries when working in RStudio?

Edit

Following very useful comments, ideally I would like to avoid explicitly indicating cited works in the attached document. As a matter of fact, I'm interested in including bibliography that will consist of some cited works but also papers that are relevant to the main document but are not explicitly referenced in the document.

Phil
  • 7,287
  • 3
  • 36
  • 66
Konrad
  • 17,740
  • 16
  • 106
  • 167
  • 1
    @zx8754 my understanding was that the `csl` file is only provided in case of specific style requirements. At this moment, I'm only interested in attaching a list of relevant publications. I can live with any referencing style as soon as it helps the readers to find the relevant work. – Konrad Oct 05 '15 at 12:07

4 Answers4

23

Here is minimal working example:

paper.Rmd

---
title: 'My Title'
author: "Me me me me!"
output: pdf_document
bibliography: references.bib
---

Application written in the R programming language [@RCoreTeam] using the Shiny framework [@Chang2015].

# REFERENCES

references.bib

@Misc{Chang2015,
  Title                    = {shiny: Web Application Framework for R. R package version 0.12.1},

  Author                   = {Chang, W. and Cheng, J. and Allaire, JJ. and Xie, Y. and McPherson, J. },
  Year                     = {2015},

  Type                     = {Computer Program},
  Url                      = {http://CRAN.R-project.org/package=shiny}
}


@Article{RCoreTeam,
  Title                    = {R: A Language and Environment for Statistical Computing},
  Author                   = {{R Core Team}},
  Year                     = {2015},

  Type                     = {Journal Article},
  Url                      = {http://www.R-project.org}
}

Console output

processing file: paper.Rmd
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS paper.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output paper.pdf --filter pandoc-citeproc --template "C:\Users\tdadaev\Documents\R\win-library\3.2\rmarkdown\rmd\latex\default.tex" --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in" --bibliography references.bib 
output file: paper.knit.md


Output created: paper.pdf

paper.pdf enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Thanks very much for showing the interest in my post and providing the comments. Is it actually necessary to include the `[@RCoreTeam] using the Shiny framework [@Chang2015].` in the document? As a matter of fact, I would simply like to attach a bibliography not a list of references. There are some papers that are relevant to the content but are not directly mentioned in the text. If this is the only way, I can always rephrase the sentence and include the in-text citation but I would be much happier with just appending list of the relevant works. – Konrad Oct 05 '15 at 11:48
  • 1
    @Konrad Now I see your problem, interesting, worth adding this point to your post. – zx8754 Oct 05 '15 at 11:51
  • 3
    @Konrad I think it has to be mentioned in the text, the idea is `.bib` file can have hundreds of refs, but only the ones mentioned in `.rmd` file gets compiled as References in pdf output. – zx8754 Oct 05 '15 at 11:54
21

The pandoc documentation says:

If you want to include items in the bibliography without actually citing them in the body text, you can define a dummy nocite metadata field and put the citations there:

---
nocite: |
  @item1, @item2
...

@item3

In this example, the document will contain a citation for item3 only, but the bibliography will contain entries for item1, item2, and item3.

The entire chunk above (from --- to ...) can go anywhere in your code. @abichat prefers to put it after the YAML or in the Reference section.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 1
    This is what I want to do, but where does this "nocite: |" go? I put it in my bibliography file but that didn't work and it doesn't seem to work in my markdown document either. Are the 3 dashes at the top necessary? And, is there anything needed to close the nocite section, like another vertical bar? I see that this is copied from the RStudio documentation page, but I couldn't find any additional information there to answer my question. – svannoy Apr 04 '17 at 17:16
  • 1
    You have to put the whole code (from `---` to `...`) anywhere in your code (I prefer put it after the YAML [and then see a `---\n---` in my code] or in the Reference section) – abichat May 09 '18 at 09:53
14

If you want to include all references in the bibtex file, you can use, as mentioned by frankyan here

---
title: 'My Title'
author: "Me me me me!"
output: pdf_document
bibliography: references.bib
nocite: '@*'
---

The @* is a wildcard for all references.

Rainer
  • 8,347
  • 1
  • 23
  • 28
2

To get a bibliographic entry instead of a "comment"-like style use results="asis" in the chunk header, i.e.

```{r generateBibliography, results="asis", echo=FALSE}
require("knitcitations")
cleanbib()
options("citation_format" = "pandoc")
read.bibtex(file = "bibliography.bib")
``` 
drT
  • 123
  • 3
  • 3
  • 10