I think the question is relatively straightforward; I want to be able to write an R function that can be included in a code chunk in a R Markdown document; and the function should be able to return code in another language (e.g. HTML, LaTeX, markdown) which is then rendered as that code, and not just returned as R output.
For example, if rendering an html_document:
---
output: html_document
---
```{r}
foo <- function(text){
x <- glue::glue('<strong>{text}</strong>')
x
}
```
```{r}
foo('Here is my text')
htmltools::tag('strong', 'Here is my text')
```
When executed, the output from function foo() is returned as verbatim output
## <strong>Here is my text</strong>
While the htmltools function returns Here is my text
I have tried looking through the source code of htmltools to determine why its output gets rendered as HTML and my toy function simply treated as console output. Any insights?
Thanks for the reply, @jm_t. It is true that including results = 'asis' in the code chunk will return the output of the function exactly as is, rather than wrapped in verbatim tags. I have tried this and it does indeed work. However, the htmltools functions and other similar functions do not require this chunk option, so I am still interested in what makes those functions work out-of-the box.
Sorry I misunderstood you question. It is a good one and I would like also to know the answer.
At one point, I would like to use this to go around the limitations of block2 in Bookdown. Just need a function to start / end html div and latex environment depending of the rendering option.