user1830307
user1830307

Reputation:

Images in Shiny through R package

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.

Upvotes: 0

Views: 801

Answers (2)

David Fong
David Fong

Reputation: 546

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"]], ... )})

Upvotes: 2

Andrew Chisholm
Andrew Chisholm

Reputation: 6567

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

Andrew

Upvotes: 5

Related Questions