If you set a chunk option using the traditional arg = value
style then it's fine, but the new YAML style (arg: value
) doesn't parse R variables, either as part of a glue string or as inline R code. This would be a useful thing to have, for captions and alt text for example.
I was going to create an issue on the rmarkdown github repo, but Yihui says to ask on here or SO first.
In the examples below, p1
works fine with the existing style. p2
doesn't parse the {variables} in a glue string for the caption, and in p3
R inline code in backticks also isn't parsed (I thought it was worth a try - in RMarkdown YAML headers you can do something like date: "`r Sys.Date()`"
, so I wondered if this would work for the new style YAML-ish chunk options. But no.)
I also tried using the ! expr
prefix trick but this just gave an error.
Is this something that should be implemented or are there reasons why it's not working here, or can't work?
```{r init, echo = TRUE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE)
library(dplyr)
library(ggplot2)
library(glue)
n <- 4
y <- sample(txhousing$year, 1)
p <- txhousing %>%
filter(year == y) %>%
filter(city %in% sample(unique(city), n)) %>%
mutate(month = as.factor(month)) %>%
ggplot(aes(x = month, y = median)) +
geom_point(aes(colour = city)) +
scale_colour_brewer(type = "qual", palette = "Dark2") +
labs(title = glue("Median house prices in {n} Texas cities, {y}"))
```
```{r p1}
#| fig.cap = glue("Median house prices in {n} Texas cities, {y}") # works
p
```
Median house prices in 4 Texas cities, 2013
```{r p2}
#| fig.cap: glue("Median house prices in {n} Texas cities, {y}")
p
```
glue("Median house prices in {n} Texas cities, {y}")
```{r p3}
#| fig.cap: "Median house prices in `r n` Texas cities, `r y`"
p
```
Median house prices in
r n
Texas cities, r y
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19043)
##
## Locale:
## LC_COLLATE=English_United Kingdom.1252
## LC_CTYPE=English_United Kingdom.1252
## LC_MONETARY=English_United Kingdom.1252
## LC_NUMERIC=C
## LC_TIME=English_United Kingdom.1252
##
## Package version:
## evaluate_0.14 glue_1.4.2 graphics_4.1.1 grDevices_4.1.1
## highr_0.9 knitr_1.36.7 magrittr_2.0.1 methods_4.1.1
## stats_4.1.1 stringi_1.7.5 stringr_1.4.0 tools_4.1.1
## utils_4.1.1 xfun_0.27 yaml_2.2.1
Created on 2021-11-01 by the reprex package (v2.0.1)