如何使用DT、R和Shiny将图像嵌入到表格的单元格中

k7fdbhmy  于 2023-01-10  发布在  其他
关注(0)|答案(3)|浏览(177)

如何将图像嵌入使用DT包生成的单元格中,以便使用shiny在应用中显示?
我的示例基于以下问题R shiny: How do I put local images in shiny tables
下面的示例代码不显示图像,而只显示url。

# ui.R
require(shiny)
library(DT)

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

# Server.R
library(shiny)
library(DT)

dat <- data.frame(
  country = c('USA', 'China'),
  flag = c('<img src="test.png" height="52"></img>',
           '<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Flag_of_the_People%27s_Republic_of_China.svg/200px-Flag_of_the_People%27s_Republic_of_China.svg.png" height="52"></img>'
           )
)

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

    DT::datatable(dat)
  })
})
06odsfpq

06odsfpq1#

您可以在DT呼叫中使用escape = FALSE,如下所示:https://rstudio.github.io/DT/#escaping-table-content

# ui.R
require(shiny)
library(DT)

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

# Server.R
library(shiny)
library(DT)

dat <- data.frame(
  country = c('USA', 'China'),
  flag = c('<img src="test.png" height="52"></img>',
           '<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Flag_of_the_People%27s_Republic_of_China.svg/200px-Flag_of_the_People%27s_Republic_of_China.svg.png" height="52"></img>'
           )
)

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

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

u5rb5r59

u5rb5r592#

自2021年起的微小更新:

require(shiny)
library(DT)

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

dat <- data.frame(
  country = c('China'),
  flag = c('<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Flag_of_the_People%27s_Republic_of_China.svg/200px-Flag_of_the_People%27s_Republic_of_China.svg.png" height="52"></img>'
  )
)

#now this is a function
shinyServer <- function(input, output){
  output$mytable <- DT::renderDataTable({
    
    DT::datatable(dat, escape = FALSE) # HERE
  })
}

#minor change to make it runnable
shinyApp(shinyUI, shinyServer)
xytpbqjk

xytpbqjk3#

对我有效的解决方案如下:函数被分配给shinyServer shinyServer <- function,而不是以前使用shinyServer(function(input,output)的方式

相关问题