Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created June 24, 2013 19:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ramnathv/f476dd3751e672a96801 to your computer and use it in GitHub Desktop.
Save ramnathv/f476dd3751e672a96801 to your computer and use it in GitHub Desktop.
Images in Tables in Shiny
require(shiny)
shinyServer(function(input, output){
output$mytable <- renderTable({
dat <- data.frame(
country = c('USA', 'China'),
flag = c('<img src="http://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/200px-Flag_of_the_United_States.svg.png"></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"></img>')
)
dat
}, sanitize.text.function = function(x) x)
})
require(shiny)
shinyUI(
tableOutput('mytable')
)
@hack-r
Copy link

hack-r commented Dec 29, 2014

Brilliant!

Is it possible to make this work with reactive given that you cannot coerce class ""reactive"" to a data.frame?

I was trying this...

searchResult <- reactive({
  subset(df, grepl(input$text1, df$Title))
})

with this

output$searchResult <- renderTable({
  dat <- data.frame(searchResult)}, sanitize.text.function = function(x) x)
    }

@hack-r
Copy link

hack-r commented Dec 29, 2014

Nevermind -- I figured it out! :)

@mikejruane
Copy link

Thanks for this. I've been struggling with getting images into a table for days now!

This allows me to get a fixed image into a table, but how do you make this variable, i.e. dependent on a column of the table?

I have a simple app that effectively subsets a data frame depending on what the user is looking for. For each row of data, there is an image saved in a folder named "www" in the directory, as instructted in the Shiny tutorial. The images are saved as "the-first-column-of-the-dataset.JPG". Below is the Server.R code I'm working with:

CPdata <- read.csv("data/Inventory Data Update 3.csv",
check.names = F,
stringsAsFactors = F)
library(shiny)
shinyServer(function(input, output) {

    output$result <- renderTable({

            if(input$enter1 != "" & input$enter2 != "")
            {x <- subset(CPdata,
                         grepl(tolower(input$enter1), tolower(CPdata[, input$search])), 
                         c(names(CPdata)[1], input$search, if(input$enter2 != "")
                         {names(CPdata)[grepl(tolower(input$enter2), tolower(names(CPdata)))]}))

             pic <- paste(x[,1], ".JPG", sep = "")

             if(input$search != names(CPdata)[1])
             {df <- data.frame("Inventory Number" = x[,1], 
                         subset(CPdata,
                                grepl(tolower(input$enter1), tolower(CPdata[, input$search])), 
                                c(input$search, if(input$enter2 != "")
                                {names(CPdata)[grepl(tolower(input$enter2), tolower(names(CPdata)))]})),
                         Work = '<img src=pic.JPG></img>',
                         row.names = NULL,
                         check.names = F)
             df}
             else
             {df <- data.frame(subset(CPdata,
                                CPdata[,input$search] == input$enter1, 
                                c(input$search, if(input$enter2 != "")
                                {names(CPdata)[grepl(tolower(input$enter2), tolower(names(CPdata)))]})),
                         Work = '<img src=pic.JPG></img>',
                         row.names = NULL,
                         check.names = F)}}
    }, sanitize.text.function = function(x) x)

})

on line 21 I tried to create the variable "pic" and place it in the html code in the "Work" column of the data frame. Since the html code is wrapped in quotes, it simply reads this as "pic.JPG", rather than "value-of-pic.JPG".

If anyone can shine some lighy on how I do this, it'd be much appreciated!!

Thanks,

Mike

@mikejruane
Copy link

sorry, I realise that, if adding the desired variable pic to the html code above would return "value-of-pic.JPG.JPG", but I'm sure it doesn't work with the correction "Work = '' for the reasons stated above.

Thanks,

Mike

@mikejruane
Copy link

Hi there,

I managed to solve this with the qq function, a Perl like function in the GetoptLong package on variable interpolation within text strings.

Thanks,

Mike

@ulwvfje
Copy link

ulwvfje commented Mar 8, 2016

it's really helpful for me , Thank you so much

@GaborSzalai
Copy link

Thanks a lot! Great trick!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment