1

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)
'``
Alvaro Morales
  • 1,845
  • 3
  • 12
  • 21
  • 1
    Does this answer your question? [how to create a loop that includes both a code chunk and text with knitr in R](https://stackoverflow.com/questions/36373630/how-to-create-a-loop-that-includes-both-a-code-chunk-and-text-with-knitr-in-r) – Lief Esbenshade May 06 '21 at 18:53
  • yes, it does, tks. – Alvaro Morales May 07 '21 at 01:47

1 Answers1

0

You could have a long string as a variable, have R values displayed in inline R code in Rmarkdown with `r your_r_code_here`.

```{r }
longstring <- 'We random sampled ' 
random <-  runif(1, 1, 99)
cat(paste(longstring, random, "units"))
```

And you can turn that into inline R code since the variables have been created in a previous chunk like this

`r paste0(longstring, random, ' units')`

enter image description here

Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27