2017-06-06 276 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