我正在尝试将 Shiny 应用程序转换为 R 包,但我在处理有关 www 目录以及“松散”文件的所有问题时遇到了问题。
我闪亮的应用程序运行得很好,但是当我尝试“打包它”时,它不起作用。
我闪亮的应用程序目录:
+---my_shiny
| +---app.R
| +---utils.R
| +---www
| +---style.css
| +---icon1.png
| +---icon2.png
| +---icon3.png
| +---font.ttf
我的代码是这样开始的:
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",
现在我不知道如何将其转换为一个包,我可以在其中调用 myapp::app demo() 并部署我的应用程序。
+---myapp
| +---DESCRIPTION
| +---NAMESPACE
| +---R
| +---appdemo.R
| +---inst
| +---shiny/
| +---app.R
| +---utils.R
| +---style.css
| +---icon1.png
| +---icon2.png
| +---icon3.png
| +---font.ttf
但它不起作用,我不知道如何做到这一点。
我对所使用的方法进行了逆向工程{golem} 框架 https://engineering-shiny.org/golem.html找到一个似乎有效的方法:
-
您通常放入的所有内容www
现在应该进去inst/app/www
。在封装开发中,inst
文件夹用于存放您想要包含在包中的任意附加文件。
-
Add inst/app/www
作为“资源路径”,如下所示:
resources <- system.file("app/www", package = "my-package-name")
addResourcePath("www", resources)
-
在应用程序的 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")
)
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)