【问题标题】:How to display image inside column with DT in R Markdown or shiny?如何在 R Markdown 或闪亮的 DT 中显示列内的图像?
【发布时间】:2017-11-06 23:12:00
【问题描述】:
---
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')
                    )
)
```

我有图片网址。我尝试在 rmarkdown 或闪亮的应用程序中插入图像。我更喜欢DT 库,因为那时我可以下载 excel 文件。我该怎么做?

PS:我也想提供 excel 或 pdf 文件供我的用户下载。

我尝试使用DataTables Options,并找到related question。我尝试了下面的代码,它显示错误。

---
    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]+'"/>';',
                                                   '}'
                                               )
                        ))
                    )
)

【问题讨论】:

    标签: r shiny r-markdown dt


    【解决方案1】:

    您可以使用html标签并使用escape = FALSE,如下所示:

    ---
    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)
    ```
    

    你会得到这样的东西:

    【讨论】:

    • 非常感谢。您的解决方案有效。是否可以提供显示结果的excel或pdf文件?
    • 我不确定。
    • 如果回答了你的问题,你能接受吗?
    • 是的。这对我帮助很大。
    猜你喜欢
    • 2021-09-02
    • 2016-01-02
    • 1970-01-01
    • 2017-03-06
    • 2017-08-08
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    相关资源
    最近更新 更多