Apricot Apricot - 2 months ago 10
R Question

How to display images retrieved from MySQL in R Shiny?

I get data from an android app which allows users to take pictures and upload it to a server.
The image is stored in MySQL in JPEG format.
I get the following data for every image...



data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wAARCABkAGQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDl4LJZyo8zYWfbjjPbnr7/AOecIbbygZFkZcDq3yk/TBOR70kMMjAOjqPn2jMoBB/E9PemTr5L7GwSePlYH9RxTJJPsT5Yb+jbQQpIORkc+lRFpEVoMjGckbR1+tPijmO4x7jy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6kUvP0FIBsgzG30qe3RjM0SyrEGUhixwpAGcfpx74qIelRqWZEUZGBj3JoAs3UAhXYJY5gybsxnI+h9+Kbps4gu4ZWXIRwTnPFRywyxPsk4OAcexGR+hpqghNuepzmgB7ryyZz2yDUmnSmO5iYbRtbGXztAPHOO3NQbT/AH6RTtLJjHPX2oAu3FkscJf7TBIcD5VY5P8Anjr/AI1XhG8ou4Lk4yeg+tJtHB3ZyPyoQAZXNAFyPT55FyGgx7zoP5miqYopDFWIEBiuQeKbdKqQgqADnGRVhLpvsoty6eWH3DgZzjHXrj2qC6kVoAoIJDZOPSmIbbrJJsjUMznCqqjk+gFXZdOu7aFZrm1lijZtoLqRz+P+evpVCPhck+n8qlWVR3oQDAfmdSOh4PtSKPxPP86Ac3LEfdI60pTrjuc/ypAWbt4pQJRNPLMx+Yy88Y45zzj1qtSbD2H5mk2t6D86Bj/xpjkbh78UoVu4H/fX/wBagqTjpwaBFu2ezCFbpH3bgwZOpHocnGPwzz1qseoweaCCSTlcn1pCp6ggH6UASMoB5yM88UU0hmOcrz7UUDGeS+ei/wDfX/1qXySRglQKsYzSAce9MRD5TbQu5ePUUCI/7P5f/XqXvkfpR/OgCNU2sTn8BS08Y7ZpMc0gG0d6WigYmAKB1o+lKOvvmgBOv40UoFJ0oATHoDRSiigCSk7Z96KKYhScqzGm9ce9FFIBF6E+lOAyeaKKBje9Oxzj2oooAbijtRRQAUlFFAC44ooooA//2Q==

When I render it in shiny,I get only the blank template for each line whereas the pic is not getting loaded.
I am not sure if I should remove the initial tag

data:image/jpeg;base64,


to get only the picture...but not sure if it will help.

My current code to render the images in inside renderUI is as follows:

title = HTML(paste0("<div class='image-wrap'><img src='./images/",
ps()$img[ps()$image_path == i],
"'class=fixed-height'",
ps()$img[ps()$image_path == i],
"'></div>"

))


The same code works well when I render locally stored jpeg images.

This is the first time I am working with images, I am not sure if the given details are sufficient enough.

Answer

I'm not sure if this is what you want, but if you just want to output the given encoded image to the webpage, then you can simply use that base64 string as the src attribute of an <img> tag. Here's an example to get your started:

library(shiny)

imageData <- "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wAARCABkAGQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDl4LJZyo8zYWfbjjPbnr7/AOecIbbygZFkZcDq3yk/TBOR70kMMjAOjqPn2jMoBB/E9PemTr5L7GwSePlYH9RxTJJPsT5Yb+jbQQpIORkc+lRFpEVoMjGckbR1+tPijmO4x7jyF+Xvnt+lNdGjYow2sMghhyKmzuPSw1VJVjkcDP64/rSA4OV6+1KMjP68U+S3mi3e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YJY5gybsxnI+h9+Kbps4gu4ZWXIRwTnPFRywyxPsk4OAcexGR+hpqghNuepzmgB7ryyZz2yDUmnSmO5iYbRtbGXztAPHOO3NQbT/AH6RTtLJjHPX2oAu3FkscJf7TBIcD5VY5P8Anjr/AI1XhG8ou4Lk4yeg+tJtHB3ZyPyoQAZXNAFyPT55FyGgx7zoP5miqYopDFWIEBiuQeKbdKqQgqADnGRVhLpvsoty6eWH3DgZzjHXrj2qC6kVoAoIJDZOPSmIbbrJJsjUMznCqqjk+gFXZdOu7aFZrm1lijZtoLqRz+P+evpVCPhck+n8qlWVR3oQDAfmdSOh4PtSKPxPP86Ac3LEfdI60pTrjuc/ypAWbt4pQJRNPLMx+Yy88Y45zzj1qtSbD2H5mk2t6D86Bj/xpjkbh78UoVu4H/fX/wBagqTjpwaBFu2ezCFbpH3bgwZOpHocnGPwzz1qseoweaCCSTlcn1pCp6ggH6UASMoB5yM88UU0hmOcrz7UUDGeS+ei/wDfX/1qXySRglQKsYzSAce9MRD5TbQu5ePUUCI/7P5f/XqXvkfpR/OgCNU2sTn8BS08Y7ZpMc0gG0d6WigYmAKB1o+lKOvvmgBOv40UoFJ0oATHoDRSiigCSk7Z96KKYhScqzGm9ce9FFIBF6E+lOAyeaKKBje9Oxzj2oooAbijtRRQAUlFFAC44ooooA//2Q=="

ui <- fluidPage(
  tags$img(src = imageData)
)

server <- function(input, output, session) {

}

shinyApp(ui = ui, server = server)
Comments