Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny: How do I put local images in shiny tables

Tags:

jquery

r

shiny

I can't seem to get shiny to display an image in a table if the image is stored on my machine. I can get it to work if the image is specified by URL, but I do not know of any simple way to "serve" the image so that it can be found by URL as opposed to just using the image file name and putting the file in same directory as ui.R and server.R (I have tried putting the image in R's home directory as well). I have checked the HTML that is output and it appears to be just the same as that in HTML docs which succeed at displaying images stored on the local machine. In fact, if I copy and save the output HTML from shiny as a static page, with the image in the same dir, then open that in my browser, the image does display.

The server.R and ui.R scripts I am using are pasted below.

Does anyone know of a good work-around or other solution?

Thank you for your help.

The server.R script:

#server.R
require(shiny)
shinyServer(function(input, output){
    output$mytable <- renderTable({
        dat <- data.frame(
            country = c('USA', 'China','Working directory'),
            flag = c('<img src="test.png" height="52"></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" height="52"></img>',
                             getwd())
        )
        dat
    }, sanitize.text.function = function(x) x)
})

The ui.R script:

require(shiny)

shinyUI(
    tableOutput('mytable')
)
like image 615
samhiggins2001 Avatar asked Nov 10 '22 16:11

samhiggins2001


1 Answers

The URL to an item on your machine is just something along these lines, which I could use to bring up the "Introduction to R" on my machine:

 http://127.0.0.1:19812/doc/manual/R-intro.html

So you need to determine the proper directory/path to place between 127.0.0.1:19812 and the file name (, and also check your port numbers.)

like image 115
IRTFM Avatar answered Nov 15 '22 06:11

IRTFM