function to generate both markdown and r code?

I have this:

# Title 1

```{r}
barplot(mtcars$mpg)

```
text describing plot 1

# Title 2

```{r}
barplot(mtcars$mpg)

```
text describing plot 2

# Title 3

```{r}
barplot(mtcars$mpg)

```
text describing plot 3

As you see, the unit of repetition is:

# Title x

```{r}
barplot(mtcars$mpg)

```
text describing plot x

Is there a way to create a function that generates this unit of repetition?

Expected input that equals what I have above:


```{r}
function_that_creates_title_graph_text(1)

function_that_creates_title_graph_text(2)

function_that_creates_title_graph_text(3)

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

Here are some recipes to help with this kind of task

The second one is about knitr::knit_expand which is like glue and allow to generate Rmd source easily

Hope it helps

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.