Home > Back-end >  I am unable to knit R Markdown document when bibliography is specified in relative path
I am unable to knit R Markdown document when bibliography is specified in relative path

Time:04-27

I am trying to knit an R Markdown document inside an RStudio project. The YAML header is as follows:

---
title: "Report"
author: "Author 1"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: pdf_document
bibliography: references.bib
#csl: apa.csl
nocite: '@*'
---

When I try to knit this, I get an error message - File references.bib not found in resource path Error: pandoc document conversion failed with error 99 Execution halted

But when I change the YAML header to following lines, I can knit the document correctly.

---
title: "Report"
author: "Author 1"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: pdf_document
bibliography: "absolute/path/to/references.bib"
#csl: apa.csl
nocite: '@*'
---

My session info is as follows:

xfun::session_info(packages = "rmarkdown")
R version 4.2.0 (2022-04-22 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044), RStudio 2022.6.0.322

Locale:
  LC_COLLATE=English_India.utf8 
  LC_CTYPE=English_India.utf8   
  LC_MONETARY=English_India.utf8
  LC_NUMERIC=C                  
  LC_TIME=English_India.utf8    

Package version:
  base64enc_0.1.3 bslib_0.3.1     digest_0.6.29  
  evaluate_0.15   fastmap_1.1.0   fs_1.5.2       
  glue_1.6.2      graphics_4.2.0  grDevices_4.2.0
  highr_0.9       htmltools_0.5.2 jquerylib_0.1.4
  jsonlite_1.8.0  knitr_1.38      magrittr_2.0.3 
  methods_4.2.0   R6_2.5.1        rappdirs_0.3.3 
  rlang_1.0.2     rmarkdown_2.14  sass_0.4.1     
  stats_4.2.0     stringi_1.7.6   stringr_1.4.0  
  tinytex_0.38    tools_4.2.0     utils_4.2.0    
  xfun_0.30       yaml_2.3.5     

Pandoc version: 2.18

According to R Markdown Cookbook, I just need to specify the filename of the references.bib. I don't have to specify the absolute path of the file. What am I doing wrong here?

CodePudding user response:

You can only include references.bib as-is in your header if the bib file is in the same directory location as the .Rmd file, i.e., if they are in the same folder. If they are not in the same folder, you need to tell R where to find the bibliography.

Suppose you had two references.bib files in different locations on your computer. How would R know which one you wanted to use if you didn't tell it?

The R Markdown Cookbook covers more information on working directories in section 16.6 The working directory for R code chunks.

CodePudding user response:

I found the culprit in my case. When I looked at the reference.bib file by right clicking > Properties, the actual file name was showing "reference.bib.bib". When I changed the file name, I can again knit the .Rmd file properly.

Thank you @Molly OW and @rawr for your support.

  • Related