闪亮的应用程序包:css 和所有 www/ 目录内容

Pau*_*ula 7 css r package shiny

我正在尝试将 Shiny 应用程序转换为 R 包,但我在处理有关 www 目录以及“松散”文件的所有问题时遇到了麻烦。

我闪亮的应用程序运行完美,但当我尝试“打包”它时,它不起作用。

我闪亮的应用程序目录:

+---my_shiny
|   +---app.R
|   +---utils.R
|   +---www
|       +---style.css
|       +---icon1.png
|       +---icon2.png
|       +---icon3.png
|       +---font.ttf
Run Code Online (Sandbox Code Playgroud)

我的代码是这样开始的:

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",
Run Code Online (Sandbox Code Playgroud)

现在我不知道如何将其转换为一个包,我可以在其中调用 myapp::appdemo() 并部署我的应用程序。

+---myapp
|   +---DESCRIPTION
|   +---NAMESPACE  
|   +---R
|       +---appdemo.R
|   +---inst
|       +---shiny/
|           +---app.R
|           +---utils.R
|           +---style.css
|           +---icon1.png
|           +---icon2.png
|           +---icon3.png
|           +---font.ttf
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我不知道如何做到这一点。

Blu*_*oxe 3

我对{golem} 框架使用的方法进行了逆向工程,以找到一种似乎有效的方法:

  • 您通常放入的所有内容www现在都应该放入inst/app/www。在包开发中,该inst文件夹用于存放您想要包含在包中的任意附加文件。

  • 添加inst/app/www为“资源路径”,如下所示:

    resources <- system.file("app/www", package = "my-package-name")
    addResourcePath("www", resources)
    
    Run Code Online (Sandbox Code Playgroud)
  • 在应用程序的 UI 中使用以下内容来嵌入资源:

    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")
      )
    
    )
    
    Run Code Online (Sandbox Code Playgroud)

编辑

我一直在一个包上测试这个,当安装包时,上面的内容似乎不再起作用(尽管它可以与 一起使用devtools::load_all())。

要修复此问题,您需要在包的调用中添加资源.onLoad(),该资源通常应位于R/zzz.R

resources <- system.file("app/www", package = "my-package-name")
addResourcePath("www", resources)
Run Code Online (Sandbox Code Playgroud)