2017-06-06 220 views
0
--- 
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文件。我怎样才能做到这一点?如何在R Markdown或闪亮的DT列中显示图像?

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

回答

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

你会得到这样的事情: enter image description here

+0

非常感谢你。你的解决方案有效是否可以提供显示结果的excel或pdf文件? –

+0

我不确定。 – SBista

+1

如果回答你的问题,你能接受吗? – SBista