如何使用R和SHINK将图像嵌入表格的单元格中?
我正在尝试创建一个图书目录,需要帮助在表的单元格中呈现shiny上的图像。我正在使用的代码如下,我从shiny应用程序上的代码获得的输出是一个带有“image”列的表,但在其单元格中包含图像的链接,而不是图像。我如何更正此问题?请帮助我 数据集中的URL采用以下格式: 数据如下所示如何使用R和SHINK将图像嵌入表格的单元格中?,r,shiny,shinydashboard,shiny-server,shiny-reactivity,R,Shiny,Shinydashboard,Shiny Server,Shiny Reactivity,我正在尝试创建一个图书目录,需要帮助在表的单元格中呈现shiny上的图像。我正在使用的代码如下,我从shiny应用程序上的代码获得的输出是一个带有“image”列的表,但在其单元格中包含图像的链接,而不是图像。我如何更正此问题?请帮助我 数据集中的URL采用以下格式: 数据如下所示 标题作者评分\u计数平均值\u评分图像\u url 惠普JK 10 4https://images.gr-assets.com/books/1447303603s/2767052.jpg ui <- fluid
标题作者评分\u计数平均值\u评分图像\u url
惠普JK 10 4https://images.gr-assets.com/books/1447303603s/2767052.jpg
ui <- fluidPage(
####Heading##
titlePanel(div(HTML("<b> Interested In books? </b>"))),
###Creating tabs###
tabsetPanel(
####First tab for crime####
tabPanel(" Book Directory ",
sidebarLayout(
sidebarPanel(
#First Input##
selectizeInput(inputId = "Book",
label = " Choose a Book",
choices = book_names)),
##Output
mainPanel = (tableOutput("View")
)
)
)
)
)
###Server app
server <- function(input, output) {
output$View <- renderTable({
books1 <- books[books$title%in% input$Book,]
books1 %>%
mutate(image = paste0('<img src="', image_url, '"></img>')) %>%
select(image,title,authors,average_rating,ratings_count)
})
}
shinyApp(ui = ui, server = server)
ui我以前用tableHTML包做过类似的事情,事实上你也可以用它向你的表中添加各种格式,例如:
库和示例数据
library(tableHTML)
library(shiny)
library(dplyr)
books <- read.table(text = "title authors ratings_count average_rating image_url
HP JK 10 4 https://images.gr-assets.com/books/1447303603s/2767052.jpg", header=TRUE)
books_names <- unique(books$title)
您可以使用add\u css\u…
函数系列以任何方式格式化表格,例如add\u css\u表格(css=list('text-align','center'))
通过表格将文本与中心对齐
查看该软件包,查看该软件包提供的其他功能我尝试了这个方法,效果很好。但是我的表输出没有对齐。例如,表格中单元格的内容完全左对齐。如何解决此问题?>谢谢您的回答。但是,这会对齐我的文本,但不会对齐表格的标题。我应该添加什么来对齐我的列名(居中)?另外,如何增加单元格中显示字段的字体大小?请建议可以做什么?您也可以简单地为标题添加css,尝试添加%%>%add_css_标题(css=list(c('text-align','font-size'),c('center','15px')),标题=1:ncol(books))
我真的建议您看看这些示例和软件包的文档。这很完美。我会看看文档。谢谢您很高兴能提供帮助!如果我的解决方案解决了你的问题,请接受它作为答案。
ui <- fluidPage(
titlePanel(div(HTML("<b> Interested In books? </b>"))),
tabsetPanel(
tabPanel(" Book Directory ",
sidebarLayout(
sidebarPanel(
selectizeInput(inputId = "Book",
label = " Choose a Book",
choices = books_names)),
mainPanel = (tableOutput("View"))
)
)
)
)
server <- function(input, output) {
output$View <- render_tableHTML({
books[books$title%in% input$Book,] %>%
mutate(image = paste0('<img src="', image_url, '"></img>')) %>%
select(image,title,authors,average_rating,ratings_count) %>%
tableHTML(escape = FALSE,
rownames = FALSE,
widths = c(40, 40, 65, 120, 120)) %>%
# align the text like this
add_css_table(css = list('text-align', 'center'))
# you can also add a theme
# add_theme('scientific')
})
}
shinyApp(ui = ui, server = server)