How to embed an image in a cell a table using R and Shiny?

I am trying to create a book directory and need help rendering image on shiny in a cell in a table.The code I am using is below and the output I am getting from the code on a shiny app is a table with the column 'image' but includes the link for the image in its cell instead of the Image.How can I correct this?Please help me The URL in the dataset are in this format : https://images.gr-assets.com/books/1447303603s/2767052.jpg

The data looks like this

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)

Upvotes: 0

Views: 872

Answers (1)

DS_UNI
DS_UNI

Reputation: 2650

I did something like this before with the package tableHTML, in fact you can also add all sorts of formatting to your table with it, try this for example:

Libraries and sample data

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 (The same 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:

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')
  })
}

Run app:

shinyApp(ui = ui, server = server)

you can format your table any way you like with the add_css_... family of functions, for example add_css_table(css = list('text-align', 'center')) aligns the text to the center through out the table.

Take a look at the package's vignettes to see the other functions the package offers

Upvotes: 2

Related Questions