6

If you want to include an image in a normal shiny app, you would call

shiny::img(src = "imgName.png")

in your ui function with the following directory structure:

| shinyApp/
    | ui.R
    | server.R
| www/
    | myImage.png

How do you replicate this in a shiny app that is also an r package? I've tried to do everything exactly the same, but with the following directory structure, with no luck:

| packageName/
    | R
        | app.R # contains ui.R and server.R
    | inst
        | www
            | imgName.png

For what it's worth, in my case the package is actually bundling a shiny module, but I don't think that's relevant to this issue.

EDIT: Minimal example demonstrating my issue, using this package.

tbadams45
  • 848
  • 1
  • 8
  • 19
  • Never done it myself, but does [this answer help](http://stackoverflow.com/a/21998722/3220769)? – TomNash Aug 05 '16 at 14:29
  • That's the way to do it for a normal app (the first way I described in my question), but it doesn't work when it's in a package. – tbadams45 Aug 05 '16 at 14:40

3 Answers3

6

Use addResourcePath to add a directory to be visible to the Shiny web sever.

As is stated on the documentation https://shiny.rstudio.com/reference/shiny/1.0.2/addResourcePath.html

Adds a directory of static resources to Shiny's web server, with the given path prefix. Primarily intended for package authors to make supporting JavaScript/CSS files available to their components.

It should allow you make references to any file in your package.

Geovany
  • 5,389
  • 21
  • 37
5

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 in the R/ directory.

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

where the package-name is DailyMeasure.

The image file is in inst/www/imgname.png.

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 = 'www/imgname.png', # this depends on addResourcePath in zzz.R
    subtitle = Sys.info()[["user"]], ... )})
David Fong
  • 506
  • 4
  • 3
1

You have more than one option. The easiest way is to place the png file in the inst folder and then access it with system.file("imgName.png",package="yourPackage")

Your approach should probably work if you changed the code to shiny::img(src="www/imgName.png"), but I'm not certain.

Carl
  • 5,569
  • 6
  • 39
  • 74
  • For my package `dtphase1`, with `flowchart.png` in the `inst` folder, this isn't working: `shiny::img(src = system.file("flowchart.png", package="dtphase1"))`. Is that not what you meant? – tbadams45 Aug 05 '16 at 15:14
  • Is the package installed in your library? – Carl Aug 05 '16 at 15:21
  • Yup. Installed latest version with `devtools::install_github("tbadams45/dtphase1")`. Package itself is working fine. It's just the image that's the problem. – tbadams45 Aug 05 '16 at 16:00
  • Before even debuggint the app, what happens if you enter `system.file("flowchart.png",package="dtphase1"))` from your R console? Does is give the path to the image? – Carl Aug 05 '16 at 16:08
  • Yup, "/home/tbadams45/R/x86_64-pc-linux-gnu-library/3.3/dtphase1/flowchart.png". The file's definitely there. Shiny just can't find it when I run the app. – tbadams45 Aug 05 '16 at 16:09
  • Here's a minimal example: https://gist.github.com/tbadams45/c272ae1c4b98197386456553fb507ada – tbadams45 Aug 05 '16 at 16:20
  • Just ran it and I can see the flowchart. I am guessing that you have multiple `.libPaths()` and that one of them has an earlier version of your package installed. You are the second person this week on SO having trouble getting `system.file` to work in Shiny apps. – Carl Aug 05 '16 at 16:23
  • What does `sapply(.libPaths(), list.files)` yield? – Carl Aug 05 '16 at 16:24
  • Weird. Glad to here that at least. let's [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120262/discussion-between-tbadams45-and-carl). – tbadams45 Aug 05 '16 at 16:30