Soma
Soma

Reputation: 123

Shiny can not display Image locally

Currently I am running Shiny app locally and trying to display a PNG image (Name : download.png), which is saved locally in the 'www' folder under my Working directory.

Now I want to display that image in my Browser locally, so I have below simple code (please note that I didnt save below code in my disk, I just have written this code and sitting in my R code editor - Sublime Text) :

runApp(list(
  ui = fluidPage(
   tags$img(src = 'www/download.png')
  ),
  server = function(input, output) {
  }
))

However unfortunately above code could not display the image in the Browser. However R could affirm availability of the file :

> file.exists('www/download.png')
[1] TRUE

Can someone confirm where I went wrong?

Upvotes: 0

Views: 3773

Answers (1)

LyzandeR
LyzandeR

Reputation: 37879

You can do the following, instead of having your code in your text editor:

  1. Add the following to the ui.r file:

    fluidPage(
     #notice that you don't need to use www/download.png - shiny knows
     #that it needs to look in www/
     tags$img(src = 'download.png')
    )
    
  2. Add the following to the server.r file:

    function(input, output){}
    
  3. Navigate to the directory where server.r, ui.r and www/ are (using setwd() on the R console for example) and run:

    runApp()
    

And this will work.

I am not sure why runApp(list(ui = , server = )) does not work (probably shiny does not parse www/), but using the files will work just fine.

Upvotes: 3

Related Questions