6

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.

Paula
  • 497
  • 2
  • 8
  • Have you had a look [here](https://mastering-shiny.org/scaling-packaging.html)? But I admit that this chapter is not very detailed. The only thing I see in your folder structure is that you use `inst/shiny` instead of `inst/www`. Maybe it helps if you change that? – starja Aug 13 '20 at 20:00
  • Yes, I did! I think that chapter is not finished yet and hence de "inst/www?". I partially solved the problem by: (i) putting the app and utils on R/ and (ii) by publishing the images online and reading them from the url. The css part is not that simple (I tryied inserting the css on the app itself) :( – Paula Aug 14 '20 at 13:49
  • When you install the package, a folder in `inst` gets copied to the higher level, so `inst/www` should be correct (or did I miss a change that `www` is now called `shiny`?) – starja Aug 14 '20 at 14:02
  • 1
    Any update on this? – Johan Rosa Feb 06 '22 at 10:54

1 Answers1

1

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 in inst/app/www. In package development, the inst 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")
      )
    
    )
    
wurli
  • 2,314
  • 10
  • 17