Search code examples
rshinypngshinydashboardvisnetwork

How do I load new PNG files created in a Shiny server saved to www directory?


I'm saving PNG files created in one eventReactive to a www directory for my ShinyApp. Another eventReactive is able to read those PNG outputs.

However when I re-run the first eventReactive, although it creates new files in the www directory, the second eventReactive ignores the new input and continues to display the initial PNG files.

What's the trick to point to new files in a www directory for Shiny without restarting the whole app?

What I've tried so far (in both reactive functions):

  • Deleting the www directory and re-creating it fresh with a reactive function.
  • Loading the newly created PNG files explicitly with the system.file() and img() functions.

Any help or advice much appreciated. Thanks


Solution

  • I think I figured it out (well, with a little help from a more experienced colleague):

    PNG files are being created and written out of the Shiny server, and that URL is working fine to create updated PNG files, no need to alter that.

    But, we can trick the server into treating the new set of PNGs as unique and reloading them (even though they come from the same URL), by adding a query for the system time to the PNG URI we're reading from Shiny. For example:

    ## Write out the PNG file(s) like this:
    ggsave(paste0(outPath, "/www/plot_1.png"),p, width = 20, height = 20, units = "in")
    
    ## Reading in the PNG file:
    #    This one is not going to work:
    ~image = paste0(outPath, "/www/plot_1.png")~
    #    This one correctly refreshes as an old PNG is replaced:
    image = paste0(path_to_images, "/www/plot_1.png?t=", Sys.time())
    

    The magic of this operation is that the query will be ignored by the App but will be treated appropriately by the server/browser (loaded fresh)!