0

My images are not displaying when I use shiny in a R package structure.

Within my R directory, I have a file myApp.R with a general outline as follows:

@param1
myFunction = function(param1){
  sidebar <- dashboardSidebar(...)
  body <- dashboardBody(...)
  ui <- dashboardPage(...)
  server <- function(input, output, session) { img(src='Figure1.png')}
  shinyApp(ui = ui, server = server)
}

I tried having Figure1.png inside R/www and also in inst/www, but neither location seemed to create the figures when I ran myFunction(param1). It would create the general application - but the images would just be absent.

Is there a simple workaround for this problem? Thank you.

2
  • try .png and .PNG
    – Pork Chop
    Commented Oct 19, 2017 at 8:31
  • Thanks @PorkChop. I tried changing src='Figure1.png' to src='Figure1.PNG' and also changed the actual files to have .PNG instead of .png extensions - but it did not fix the problem.
    – user1830307
    Commented Oct 19, 2017 at 16:15

2 Answers 2

5

Try the function addResourcePath("www", "www") and refer to the image with www/Figure1.png

Andrew

5
  • Thanks @awchisholm, I may be misunderstanding... but I added addResourcePath("www", "www") to the first line within myFunction() and also as the first line within server <- function(input, output, session) {}. In both cases, I received the error: "Error in normalizePath(directoryPath, mustWork = TRUE) : path[1]="www": No such file or directory"
    – user1830307
    Commented Oct 19, 2017 at 16:20
  • 1
    The second parameter is a full path; so perhaps something like ‘/home/user/www’ Commented Oct 19, 2017 at 20:23
  • Thanks again. It now works on my system. However, I am concerned this will not work as a package function as I planning to submit it to CRAN. I don't think I could use an absolute reference because of that.
    – user1830307
    Commented Oct 19, 2017 at 21:06
  • 1
    If you have files released with the package you could use the system.file() function to find the path of the file at run time and use this in the call to addResourcePath. Commented Oct 19, 2017 at 21:15
  • e.g. shiny::addResourcePath('www', system.file('www', package = 'MyPackage'))
    – chasemc
    Commented Jan 26, 2019 at 15:40
2

A working example can be found by Divad Nojnarg's "CaPO4 sim", as described in an issue I raised about referring to a local icon file in the shinydashboardPlus user description.

In summary, one way to reference local image files is by adding a zzz.R file

.onAttach <- function(libname, pkgname) {
  shiny::addResourcePath('icons',
                         system.file("www/assets/icons",
                                      package = "DailyMeasure"))
}

where my package-name is DailyMeasure.

My image file is in inst/www/assets/icons/user-avatar.svg.

The file is referenced in the server section of Shiny like this...

output$user <- shinydashboardPlus::renderUser({
  shinydashboardPlus::dashboardUser(
    name = UserConfig()$Fullname[UserConfig()$AuthIdentity == Sys.info()[["user"]]],
    src = 'icons/user-avatar.svg', # this depends on addResourcePath in zzz.R
    subtitle = Sys.info()[["user"]], ... )})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.