Including an image in a shiny app package

妖精的绣舞 提交于 2020-01-05 04:51:44

问题


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.


回答1:


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

As is stated on the documentation http://shiny.rstudio.com/reference/shiny/latest/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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/38791613/including-an-image-in-a-shiny-app-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!