【问题标题】:Embedding an image in a table cell using DT, R and Shiny not working使用 DT、R 和 Shiny 在表格单元格中嵌入图像不起作用
【发布时间】:2017-12-26 08:26:34
【问题描述】:

这个问题被问到here 并且得到了回答。但是,现在,它对我不起作用。我不确定包裹中是否有任何变化。有什么想法吗?

ui.r

require(shiny)
library(DT)

 shinyUI(
  DT::dataTableOutput('mytable')
 )

服务器.R

library(shiny)
library(DT)


dat <- data.frame(
    country = c('USA', 'China'),
    flag = c('<img src="http://flaglane.com/download/american-flag/american-
            flag-large.png" height="52"></img>',
            '<img src="https://upload.wikimedia.org/wikipedia/commons/2/2e/Flag_of_China.png" height="52"></img>'
 )
 )

  shinyServer(function(input, output){
   output$mytable <- DT::renderDataTable({

    DT::datatable(dat, escape = FALSE)
  })
})

编辑

我的感觉是,如果它在 Rstudio 查看器中不起作用,那么当我启动 Shiny 时它也不起作用。然而,我错了。当我运行该应用程序时,它可以正常工作,但在 Rstudio Viewer 中却不行。

library(shiny)
library(DT)


  dat <- data.frame(
  country = c('USA', 'China'),

  flag = c('<img src="http://flaglane.com/download/american-flag/american-
    flag-large.png" height="52"></img>',
       '<img src="https://upload.wikimedia.org/wikipedia/commons/2/2e/Flag_of_China.png" height="52"></img>'
  )
   )
DT::datatable(dat, escape = FALSE)

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    您的示例不完整。这行得通吗?

    require(shiny)
    library(DT)
    
    ui <- shinyUI(
      DT::dataTableOutput('mytable')
    )
    
    dat <- data.frame(
      country = c('USA', 'China'),
      flag = c('<img src="http://flaglane.com/download/american-flag/american-flag-large.png" height="52"></img>',
               '<img src="https://upload.wikimedia.org/wikipedia/commons/2/2e/Flag_of_China.png" height="52"></img>'
      )
    )
    
    server <- shinyServer(function(input, output){
      output$mytable <- DT::renderDataTable({
        DT::datatable(dat, escape = FALSE)
      })
    })
    
    shinyApp(ui, server)
    

    对我来说很好用。

    【讨论】:

    • 先把图片下载到自己的机器上也行吗?它不适合我。谢谢
    • 应该可以,请参阅stackoverflow.com/questions/36182535/…。确保图像在正确的文件夹中。
    • 谢谢!关键是把它放在“www/”文件夹而不是“data/”文件夹
    • @FissehaBehane 你说你想知道如何在本地运行它,你能发布你使用的代码吗,因为我将图像更改为 www 文件夹并且它没有显示
    • @AlejandroAndrade:我在我的博客中使用了它。看看有没有帮助:datascience-enthusiast.com/R/Modals_data_exploration_Shiny.html
    猜你喜欢
    • 2015-08-20
    • 2019-09-04
    • 2021-03-25
    • 1970-01-01
    • 2021-12-13
    • 2017-01-21
    • 2020-11-08
    • 2016-06-10
    相关资源
    最近更新 更多