Use here::here function in yaml option.

Like bibliography: here::here("add.bib"), But I get error message.

output file: pca.knit.md

Could not find bibliography file: here::here(鈥渁dd.bib鈥?
Error running filter pandoc-citeproc:
Filter returned error status 1
错误: pandoc document conversion failed with error 83
停止执行

I think here can be used here.

Here is my yaml option.

---
title: "123"
author: "Jiaxiang Li"
date: "`r Sys.Date()`"
output: 
    github_document:
        df_print: tibble
bibliography: here::here("add.bib")
---

I'm not 100% sure if what you're doing will end up working (I haven't run the code), but, if you want to use the result of an R expression (e.g. for the date, you get the result of r Sys.Date(), you need to surround it with backticks and have an r next to the first backtick — just as you do with Sys.Date()).

1 Like

@mara I find only the relative path works here like bibliography: ../add.bib.
`

I am with @mara on this one - you need to surround as you do with Sys.Date. From what you have now, this is working. However,it depends where your file lives. Where is your file compare to you working directory / RStudio Projet ?

Sometime,it is not always a good idea to let the computer decide :slight_smile: I think the easier is to put the path of the bib files directly in YAML. :wink:

1 Like

Hi @cderv, here is my directory tree structure.

.
|-- add.bib
|-- xiaobinghui
||-- pca.Rmd
  1. add.bib is the bibtex file I store the all references there.
  2. pca.Rmd is the rmd file I cite some references from add.bib.

Here is my idea.
Maybe in the future, I will create an Rmd file in the deeper nodes, the relative path is a hard way to cite a reference from this bib file. Thus here::here is a good alternative.

And I find the input types are different in date and bibliography from the yaml options.

For output github_document.

  1. The correct way to type date is "2018-11-25" or "r Sys.Date()" ,
  2. but the input of bibliography seemmingly doesn't need " or '. I try add " or ' on the input like
    bibliography: "../add.bib" and I get an error.

I think as already mentioned you need to do the same as for the date, ie execute code in the yaml using the backtick and R syntax.

Did you try this

bibliography: `r here::here("add.bib")`

Otherwise, What did you try ?

Yes, I try it and get this error.

Error in yaml::yaml.load(string, ...) : 
  Scanner error: while scanning for the next token at line 4, column 15 found character that cannot start any token at line 4, column 15
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load_utf8 -> <Anonymous>
停止执行

Here is my yaml options.

---
title: "降维"
author: "Jiaxiang Li"
date: "`r Sys.Date()`"
output: 
    github_document:
        df_print: tibble
bibliography: `r here::here("add.bib")`
---

To prevent problems with the double quotes inside here() killing the entire line, it might be easier to do it like this, with single quotes:

bibliography: `r here::here('add.bib')`

Hi @rensa, I try it and stiil get this error.

Error in yaml::yaml.load(string, ...) : 
  Scanner error: while scanning for the next token at line 9, column 15 found character that cannot start any token at line 9, column 15
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load_utf8 -> <Anonymous>
停止执行

Here is the edited options.

---
title: "降维"
author: "Jiaxiang Li"
date: "`r Sys.Date()`"
output: 
    md_document:
        df_print: tibble
        toc: true
        toc_depth: 2
bibliography: `r here::here('add.bib')`
---

Whoops! Also wrap the statement in double quotes:

bibliography: "`r here::here('add.bib')`"
2 Likes

Thanks for your answer and patience. @rensa it works for me and I am not familiar with yaml options .

1 Like

That's okay! There's three things going on here, so it's a little complicated:

  • The outer quotes ("blah") are telling the YAML processor that this option runs all the way to the end of the line, not just to the next space;
  • The backticks and r (`r blah`) allow you to include R code in your YAML; and
  • The single quotes ('blah') are for the function argument.

The backticks are fixed—you can't use anything else for them—but if you re-use single or double quotes for more than one purpose, things get confused. If you absolutely have to nest the same kind of quotes inside each other, there are ways of escaping them, but the method of escaping characters is specific to the language, and with both R and YAML in here, it gets confusing real quick :weary: If you can, it's generally easier to use different kinds of quotes for each purpose.

3 Likes

I am with @mara on this one - you need to surround as you do with Sys.Date .

So at the end, this is exactly the same as "`r Sys.Date()`" :stuck_out_tongue_winking_eye:
I thought you already have tried it :wink:

Glad it works!

2 Likes

It is, but the system date example doesn't have any function arguments. Since most R style guides and package documentation examples use double quotes for strings, you hit problems when you try to use them unescaped in a YAML block :sob:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.