0

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(
    imageOutput("plot3")
  )
  
  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(
    imageOutput("plot3")
  )

  server <- function(input, output, session) {...
    
}

This puzzles me as many of Shiny's tutorials and demonstrations showed rendering can be done without encasing the code body in a scope.

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

William
  • 1
  • 1
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 08 '21 at 04:53
  • Please try to put picture size : `list(src = filename, contentType = 'image/jpeg', width = 400, height = 300, alt = "Alternate text")` – Clemsang Sep 08 '21 at 07:54
  • Does this answer your question? [Display locally-stored image in R Shiny](https://stackoverflow.com/questions/63838001/display-locally-stored-image-in-r-shiny) – ismirsehregal Sep 08 '21 at 09:00
  • Thank you everyone for any help that you rendered. They are very good references. I just discovered what was wrong with the code through trial and error. The directory which the shiny code app is saved 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 is successfully rendered. Thanks! – William Sep 09 '21 at 04:51

1 Answers1

0

I just discovered what was wrong with the code through trial and error. The directory which the shiny code app is saved 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 is successfully rendered.

William
  • 1
  • 1