Vlad Vlad - 1 month ago 13
R Question

Rmarkdown: writing html with variables

How do I add HTML inside a code block in Rmarkdown in order to use variables in the HTML code?

For example, this works:

```{r, echo=FALSE}
#some R code
```
<div><img src='http://example.com/image.jpg'></div>


This doesn't work:

```{r, echo=FALSE}
my_image <- "image.jpg"
html(paste0("<div><img src='http://example.com/",my_image,"'></div> ")
```

Answer

You can use tags from library(htmltools)

```{r, echo=FALSE}
library(htmltools)

my_image <- "http://example.com/myImage.jpg"
tags$div(tags$img(src = my_image))

```
Comments