Issue
I'm trying to convert a Shiny app into an R package and I am having trouble with all things regarding the www directory as well as "loose" files.
My shiny app works perfectly but when I try to "package it", it doesn't work.
My shiny app directory:
+---my_shiny
| +---app.R
| +---utils.R
| +---www
| +---style.css
| +---icon1.png
| +---icon2.png
| +---icon3.png
| +---font.ttf
My code starts like this:
library(hrbrthemes)
library(tidyverse)
library(plotly)
source(here::here("utils.R"))
theme_set(theme_personalized())
update_geom_defaults("text", list(family = theme_get()$text$family))
ui <- navbarPage(
"Example",
id = "navbar",
theme = "style.css",
Now I down't know how to convert it into a package where I can call myapp::appdemo() and it deploys my app.
+---myapp
| +---DESCRIPTION
| +---NAMESPACE
| +---R
| +---appdemo.R
| +---inst
| +---shiny/
| +---app.R
| +---utils.R
| +---style.css
| +---icon1.png
| +---icon2.png
| +---icon3.png
| +---font.ttf
But it doesn't work and I don't know how to make it to.
Solution
I reverse-engineered the approach used by the {golem} framework to find a method which seems to work:
Everything which you would normally put in
www
should now go ininst/app/www
. In package development, theinst
folder is used for arbitrary additional files that you want include in your package.Add
inst/app/www
as a 'resource path' as follows:resources <- system.file("app/www", package = "my-package-name") addResourcePath("www", resources)
Use the following in your app's UI to embed the resources:
tags$head( # Javascript resources htmltools::htmlDependency( name = "resources", version = "0.0.1", src = resources, script = list.files(resources, pattern = "\\.js$", recursive = TRUE), package = NULL, all_files = TRUE ), # CSS resources lapply( list.files(resources, pattern = "\\.css$", recursive = TRUE), function(x) tags$link(href = file.path("www", x), rel = "stylesheet") ) )
EDIT
I've been testing this on a package and the above no longer seems to work when the package is installed (although it works with devtools::load_all()
).
To fix, you need to add resources in the package's call to .onLoad()
, which should typically live in R/zzz.R
:
# R/zzz.R
.onLoad <- function(libname, pkgname) {
resources <- system.file("app/www", package = "my-package-name")
addResourcePath("www", resources)
}
Answered By - wurli
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.