Rendering A Local Image File in Shiny

Hi, I am trying to render a local image file onto the Shiny app interface using a snippet of the example code provided by Shiny website below:

ui <- fluidPage(
  server <- function(input, output, session) {
    # Send a pre-rendered image, and don't delete the image after sending it
    # NOTE: For this example to work, it would require files in a subdirectory
    # named images/
    output$plot3 <- renderImage({
      filename <- normalizePath(file.path('./images',
                                          paste('image', '2', '.jpeg', sep='')))
      # Return a list containing the filename
      list(src = filename, alt = "Alternate text")
    }, deleteFile = FALSE)
  shinyApp(ui, server)

It didn't work when I ran the application. However when I added back the interactive() function that was originally in the example code, encase the main code body, I was able to display the local image file without any problems.

if (interactive()) {
  ui <- fluidPage(

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

This puzzles me as many of Shiny's tutorials and demonstrations showed rendering can be done without encasing the code body in a scope and I see many other question regarding the similar rendering without incorporating of interactive().

Is there anyone who encounter a similar scenario such as this? Rendering a local image file into a Shiny app?

This doesn't make sense. all that interactive does is return TRUE or FALSE , so the effect would be to either run the app, or to skip running the app. It would not effect how an app performs....

You may have some other confusion. You didn't mention any errors or warnings in your example ?
There is nothing in principle wrong with your code, if the asset is in the location that the path would identify.
Is there any doubt about the working directory path?

Your issue might also simply be that your session has become corrupt in some way. Often restarting your R Session (control shift F10 in windows) will stop 'strange' behaviours.

Hi nirgrahamuk, I must apologise for leaving out the error prompt encountered. There is an error prompt when the shiny app was fired up, it was:
Listening on
Warning in normalizePath(path.expand(path), winslash, mustWork) :
**** path[1]="./www/image2.jpeg": The system cannot find the path specified****

I just discovered what wrong with the code, through trial or error. The directory which the shiny code app is saved in, is in a different location from the location where the image file is saved. So the solution is either to place a setwd([www image folder location]) or relocate image folder www to where the shiny app resides which it worked - the image was successfully rendered.


This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.