Inline R code in YAML for rmarkdown doesn't run

后端 未结 2 715
时光说笑
时光说笑 2020-12-19 07:36

I\'m trying to run inline R code in the YAML front matter before getting rmarkdown to run the file. However it isn\'t working for me. Here\'s an example:

相关标签:
2条回答
  • 2020-12-19 08:17

    This is how I solved this. I knit from RStudio. Curiously, I had to use one solution for the date and csl fields and a different solution for the bibliography field. !expr did not work in the date or csl lines (for me). And quoted r code didn't work in the bibliography line (for me). I have the bibliography and csl files in a package (inst/docs folder). rmarkdown files, which are not part of that package, use those.

    ---
    title: "Title"
    date: '`r format(Sys.time(), "%d %B, %Y")`'
    output: html_document
    bibliography: !expr system.file("docs", "my.bib", package = "MyPackage")
    csl: '`r system.file("docs", "my.csl", package = "MyPackage")`'
    ---
    
    # Introduction
    
    Yada yada [@MyRef04].
    
    # References
    

    my.bib is the BibTex file with MyRef04. csl is the style file

    This is a situation where one person maintains a package which has data, code, bibliography, etc. Others, potentially unknown to the package writer, install that package from GitHub and write or run rmarkdown files that use the package. The users almost certainly do not use Git or GitHub and I don't want them to have to download any extra files after installing the package from GitHub.

    Update: After posting the above, I happened to install markdown from GitHub because I needed something in the development version. With version ‘1.7.5’ of rmarkdown on GitHub you can use r code in the bibliography line:

    ---
    title: "Title"
    date: '`r format(Sys.time(), "%d %B, %Y")`'
    output: html_document
    bibliography: '`r system.file("docs", "my.bib", package = "MyPackage")`'
    csl: '`r system.file("docs", "my.csl", package = "MyPackage")`'
    ---
    

    To install rmarkdown from GitHub

    library(devtools)
    install_github("rstudio/rmarkdown")
    
    0 讨论(0)
  • 2020-12-19 08:27

    So I found a round about way of getting what I wanted. Rmarkdown I don't think allows R expressions/commands in the YAML, probably for a good reason. What I ended up doing was putting all the output yaml commands in a file called _output.Ryaml like so:

    beamer_presentation:
      slide_level: 2
      includes:
        in_header: "src/preamble.tex"
      pandoc_args: [
        "--bibliography", "`r paste('path/to/bib')`",
        "--variable", "classoption:xcolor=dvipsnames",
        "--variable", "fontsize:9pt"
        ]
    

    Then in the main slides.Rmd file, was something like:

    ---
    title: "**Title**"
    author: Luke
    ---
    
    <!-- slide 1 -->
    ## Intro ##
    

    Then, I can generate the slides using the R code (which I put into a Makefile):

    knitr::knit('_output.Ryaml', '_output.yaml')
    rmarkdown::render('slides.Rmd')
    unlink('_output.yaml')
    

    Seems to be working well enough. If anyone's got a better idea, let me know!

    0 讨论(0)
提交回复
热议问题