1
---
title: "Untitled"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
df <- data.frame(image = "http://www.ufotm.com/data/attachment/forum/201203/11/110705gf50r55yqcka5ffz.jpg", title = "here is a title")
DT::renderDataTable(df,
                escape = FALSE,
                rownames = FALSE,
                extensions = 'Buttons', options = list(
                    dom = 'lBfrtip',
                    buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
                    )
)
```

I have the image url. I try to insert image in a rmarkdown or shiny app. I prefer DT library because I can download excel file then. How can I do that?

PS: I want to provide excel or pdf file for my user to download as well.

I try to use DataTables Options, and find a related question. I tried the below code and it shows error.

---
    title: "Untitled"
runtime: shiny
output: html_document
---

df <- data.frame(image = "http://www.ufotm.com/data/attachment/forum/201203/11/110705gf50r55yqcka5ffz.jpg", title = "here is a title")
DT::renderDataTable(df,
                    escape = FALSE,
                    rownames = FALSE,
                    extensions = 'Buttons', options = list(
                        dom = 'lBfrtip',
                        buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
                        columnDefs = list(list(targets = 1,
                                               data = "img",
                                               render = JS(
                                                   'function ( url, type, full) {',
                                                   'return '<img height="75%" width="75%" src="'+full[7]+'"/>';',
                                                   '}'
                                               )
                        ))
                    )
)
H. Yong
  • 151
  • 11

1 Answers1

4

You can use html tags and use escape = FALSE as below:

---
title: "Untitled"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
df <- data.frame(image = c('<img src="http://www.ufotm.com/data/attachment/forum/201203/11/110705gf50r55yqcka5ffz.jpg" height="52"></img>' ), title = "here is a title")
DT::renderDataTable(df, escape = FALSE)
```

You will get something like this: enter image description here

SBista
  • 7,479
  • 1
  • 27
  • 58