如何使用 DT、R 和 Shiny 在单元格 a table 中嵌入图像
How to embed an image in a cell a table using DT, R and Shiny
如何将图像嵌入到使用 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)
})
})
您可以在 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
})
})
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)
如何将图像嵌入到使用 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)
})
})
您可以在 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
})
})
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)