5

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')
)
samhiggins2001
  • 318
  • 2
  • 12
  • 1
    What are "dataTables"? Do you mean to ask a question about `data.table`'s created with functions from the package of the same name? – IRTFM Dec 13 '13 at 21:00
  • Or maybe you are talking about the output from the `renderTable` function? – IRTFM Dec 13 '13 at 21:09
  • @DWin Thanks for catching that -- I meant to refer to the output from the `renderTable` function. – samhiggins2001 Dec 13 '13 at 21:28
  • 5
    If you put an image `img1.png` in `www/img/img1.png`, you can refer to it in `server.R` as `` – Ramnath Dec 14 '13 at 01:34
  • @Ramnath Thank you, that solves it quite well! Could you put your comment up as an 'Answer' -- I'd like to give you the credit. – samhiggins2001 Dec 14 '13 at 06:00
  • @Ramnath By the way, where should I have looked that I would have found that answer? Was it just something I missed in the regular CRAN documentation, or was it somewhere else? – samhiggins2001 Dec 14 '13 at 06:18

1 Answers1

1

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.)

IRTFM
  • 258,963
  • 21
  • 364
  • 487