Try working from this
suppressPackageStartupMessages({
library(glue)
})
cpar <- ")"
fence <- "```"
lang <- "{{r}}"
lpar <- "("
nl <- "\n"
ptype <- "barplot"
text <- "text describing plot "
title <- "# Title "
block_out <- function(x, ptype, y, z) {
glue(title,x,nl,nl,fence,lang,nl,ptype,lpar,y,"$",z,cpar,nl,fence,nl,nl,text,x)
}
block_out(1,"barplot","mtcars","mpg")
#> # Title 1
#>
#> ```{r}
#> barplot(mtcars$mpg)
#> ```
#>
#> text describing plot 1