Inline R code in YAML for rmarkdown doesn't run
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")
Quotes and inline R code in Rmarkdown YAML
The fontawesome::fa()
function returns a SVG
element.
For instance, the string returned by fontawesome::fa("twitter", fill = "steelblue")
is:
<svg style="height:0.8em;top:.04em;position:relative;fill:steelblue;" viewBox="0 0 512 512">
<path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"/>
</svg>
This string contains double quotes: this is the reason why single quoting leads to a proper result and double quoting does not perform well.
It is clear if you inspect the intermediate markdown
file using the keep_md
option:
---
title: "Untitled"
subtitle: "`r fontawesome::fa('twitter', fill = 'steelblue')` @twitter handle"
author: "My Name"
date: "7/12/2018"
output:
html_document:
keep_md: true
---
These quoting considerations can be done away using YAML literal or folded blocks:
subtitle: |
`r fontawesome::fa('twitter', fill = 'steelblue')` @twitter handle
or
subtitle: >
`r fontawesome::fa('twitter', fill = 'steelblue')` @twitter handle
Include YAML file in RMarkdown document
I think the easiest way is that you read the external file in an inline R expression, e.g.,
```yaml
`r xfun::file_string('envs/r40_tidyverse.yaml')`
```
Note that you should not use ```{yaml}
(i.e., do not add {}
to yaml
), because that would change its meaning to knitr:
- With curly braces, it means a code chunk, and a knitr language engine needs to evaluate the code chunk (but there is not a
yaml
engine yet, and your first example worked just by chance). - Without curly braces, it means a normal code block, which will not be processed by knitr.
Rmarkdown - python inline code in Rmarkdown
Not sure whether this is possible. All examples I have found use R inline code like so `r py$x` to achieve that. See e.g. the rmarkdown cookbook.
YAML current date in rmarkdown
This is a little bit tricky, but you just need to make the date
field valid in YAML by quoting the inline R expression, e.g.
date: "`r format(Sys.time(), '%d %B, %Y')`"
Then the parsing error will be gone, and the date will be generated in the markdown output so Pandoc can use the value from Sys.time()
.
Related Topics
Percentage of Overlap Between Polygons
Remove Zombie Processes Using Parallel Package
Creating a More Continuous Color Palette in R, Ggplot2, Lattice, or Latticeextra
Build Word Co-Occurence Edge List in R
Why Does ".." Work to Pass Column Names in a Character Vector Variable
Can You More Clearly Explain Lazy Evaluation in R Function Operators
Ggplot2: Dashed Line in Legend
Check Whether All Elements of a List Are in Equal in R
How to Download a Large Binary File with Rcurl *After* Server Authentication
How to Use an R Script from Github
Knitr Inline Chunk Options (No Evaluation) or Just Render Highlighted Code
Split a File Path into Folder Names Vector
How to Use the Function Curve in [R] to Graph a Normal Curve
How to Convert Camelcase to Not.Camel.Case in R
Sum Amount Last 6 Month Prior to the Date of Transaction
Display Error Instead of Plot in Shiny Web App