使用 knitcitations 在 RMarkdown 文档中包含参考书目

Posted

技术标签:

【中文标题】使用 knitcitations 在 RMarkdown 文档中包含参考书目【英文标题】:Including Bibliography in RMarkdown document with use of the knitcitations 【发布时间】:2016-01-01 23:14:03 【问题描述】:

我正在尝试使用 knitcitations 并将参考书目添加到我在 R Studio 中起草的 R Markdown 文档中。我的文档的标题如下所示:

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

我想使用以下代码在末尾添加参考书目:

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

参考文件bibliography.bib有以下内容:

@articledebarsy_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


@articlelamichhane_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

但是,生成的输出显示为 cmets,而不是书目条目:

文件是用以下代码编译的:

"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 

为简洁起见,我将路径更改为 _paper_paper


当我尝试关注advice on including bibliography in RStudio 时,生成的文档根本没有书目条目。因此我的问题是,我在哪里犯了错误,在 RStudio 中工作时如何强制生成书目条目?

编辑

遵循非常有用的 cmets,理想情况下,我希望避免在附件中明确指出引用的作品。事实上,我有兴趣将参考书目包括在内,其中将包含一些被引用的作品,以及与主要文档相关但未在文档中明确引用的论文。

【问题讨论】:

@zx8754 我的理解是csl文件仅在特定样式要求的情况下提供。目前,我只想附上相关出版物的清单。只要能帮助读者找到相关作品,我就可以接受任何参考风格。 【参考方案1】:

这是最小的工作示例:

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

@MiscChang2015,
  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



@ArticleRCoreTeam,
  Title                    = R: A Language and Environment for Statistical Computing,
  Author                   = R Core Team,
  Year                     = 2015,

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

控制台输出

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

【讨论】:

非常感谢您对我的帖子感兴趣并提供 cmets。真的有必要在文档中包含[@RCoreTeam] using the Shiny framework [@Chang2015].吗?事实上,我只想附上参考书目而不是参考文献列表。有一些论文与内容相关,但文中没有直接提及。如果这是唯一的方法,我总是可以改写句子并包含文本引用,但我会更乐意仅附加相关作品的列表。 @Konrad 现在我看到了您的问题,很有趣,值得将这一点添加到您的帖子中。 @Konrad 我认为它必须在文本中提到,想法是.bib 文件可以有数百个参考,但只有.rmd 文件中提到的那些被编译为pdf中的参考输出。【参考方案2】:

pandoc documentation 说:

如果您想在参考书目中包含项目而不在正文中实际引用它们,您可以定义一个虚拟 nocite 元数据字段并将引用放在那里:

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

@item3

在此示例中,文档将仅包含 item3 的引文,但参考书目将包含 item1、item2 和 item3 的条目。

上面的整个代码块(从---...)可以在您的代码中的任何位置出现。 @abichat 更喜欢将它放在 YAML 之后或参考部分中。

【讨论】:

这是我想做的,但是这个“nocite:|”在哪里走?我把它放在我的参考书目文件中,但这不起作用,而且它似乎也不适用于我的降价文档。顶部的3个破折号是必要的吗?而且,是否需要关闭 nocite 部分,例如另一个垂直条?我看到这是从 RStudio 文档页面复制的,但我找不到任何其他信息来回答我的问题。 您必须将整个代码(从 ---...)放在代码中的任何位置(我更喜欢将它放在 YAML 之后 [然后在我的代码中看到 ---\n---] 或在参考部分)【参考方案3】:

如果你想在 bibtex 文件中包含 所有 引用,你可以使用 frankyan 这里提到的

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

@* 是所有引用的通配符。

【讨论】:

【参考方案4】:

要获得书目条目而不是类似“评论”的样式,请在块标题中使用results="asis",即

```r generateBibliography, results="asis", echo=FALSE
require("knitcitations")
cleanbib()
options("citation_format" = "pandoc")
read.bibtex(file = "bibliography.bib")
``` 

【讨论】:

以上是关于使用 knitcitations 在 RMarkdown 文档中包含参考书目的主要内容,如果未能解决你的问题,请参考以下文章

在 spark 上使用集群和在本地使用并行操作有啥区别?

在哪里使用 CORBA 以及在哪里使用 SNMP 进行监控?

为啥在使用 unicode 时我不能在 :before :after 内容之后使用空格

在哪里使用 callable 以及在哪里使用 Runnable Interface?

在 Observable RxSwift 中使用 'asPromise()' 可以在 PromiseKit Promise 中使用吗?

可以在 SELECT 查询中使用 IF() 但不能在 UPDATE 中使用