如何使用R和Shiny将图像嵌入到单元格中?

问题描述 投票:0回答:1

我正在尝试创建一个图书目录,需要帮助在桌面上的单元格中呈现闪亮的图像。我正在使用的代码如下,我从一个闪亮的应用程序的代码获得的输出是一个带有图像列的图像'但是包括图像在其单元格中的链接而不是Image.How我可以更正吗?请帮助我数据集中的URL采用以下格式:https://images.gr-assets.com/books/1447303603s/2767052.jpg

数据看起来像这样

title authors ratings_count average_rating image_url HP JK 10 4 https://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)
r shiny shinydashboard shiny-server shiny-reactivity
1个回答
1
投票

我之前使用包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)

UI(相同的ui):

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)

你可以使用add_css_...函数系列以任何你喜欢的方式格式化你的表格,例如add_css_table(css = list('text-align', 'center'))通过表格将文本对齐到中心。

看看包装的vignettes,看看包装提供的其他功能

© www.soinside.com 2019 - 2024. All rights reserved.