-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.R
116 lines (104 loc) · 3.33 KB
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
app_dir_exists <- function(dir = "_site") {
fs::dir_exists(dir) && length(fs::dir_ls(dir) > 0)
}
build_app <- function(dir_source = "app", dir_build = "_site", overwrite = TRUE) {
if (isFALSE(overwrite) && fs::dir_exists(dir_build)) {
withr::with_options(
list(rlang_backtrace_on_error = "none"),
cli::cli_abort(
c("Output directory {dir_build} already exists.",
"Delete the directory or set {.code overwrite=TRUE}"
),
call = NULL
)
)
}
temp_dir <- fs::file_temp("fda-pilot")
on.exit(fs::dir_delete(temp_dir))
temp_dir_source <- fs::path_join(c(temp_dir, "src"))
temp_dir_source_www <- fs::path_join(c(temp_dir_source, "www"))
temp_dir_www <- fs::path_join(c(temp_dir, "www"))
temp_dir_out <- fs::path_join(c(temp_dir, "out"))
fs::dir_create(temp_dir)
fs::dir_copy(dir_source, temp_dir_source)
if (fs::dir_exists(temp_dir_source_www)) {
fs::file_move(temp_dir_source_www, temp_dir_www) # Move www out for shinylive::export to work cleanly
}
shinylive::export(temp_dir_source, temp_dir_out, verbose = FALSE, wasm_packages = TRUE)
if (fs::dir_exists(temp_dir_www)) {
fs::dir_walk(temp_dir_www, \(f) fs::file_move(f, temp_dir_out))
}
if (isTRUE(overwrite) && fs::dir_exists(dir_build)) {
fs::dir_delete(dir_build)
}
fs::dir_copy(temp_dir_out, dir_build)
}
run_app_webassembly <- function(dir = "_site", port = 7654) {
if (!fs::file_exists(fs::path(dir, "app.json"))) {
withr::with_options(
list(rlang_backtrace_on_error = "none"),
cli::cli_abort(
c("Webassembly application not detected in directory {dir}.",
"Run {.code build_app()} or {.code extract_app_bundle()} and try again"
),
call = NULL
)
)
}
httpuv::runStaticServer(dir = dir, port = port, browse = FALSE)
}
run_app_shiny <- function(dir = "app", port = 7654) {
if (!fs::file_exists(fs::path(dir, "app.R"))) {
withr::with_options(
list(rlang_backtrace_on_error = "none"),
cli::cli_abort(
c("Shiny application not detected in directory {dir}.",
"Ensure application files were copied successfully as documented in the ADRG."
),
call = NULL
)
)
}
shiny::runApp(appDir = dir, port = port)
}
create_ectd_bundle <- function(archive_name = "r4app.zip") {
archive::archive_write_files(
archive = fs::path("ectd_bundle", archive_name),
files = c(
fs::dir_ls("app", recurse = TRUE),
fs::dir_ls("app_bundle", recurse = TRUE),
"renv/.gitignore",
"renv/activate.R",
"renv/settings.json",
"utils.R",
"renv.lock",
"submissions-pilot4-webr.Rproj",
".Rprofile"
),
format = "zip"
)
invisible(TRUE)
}
create_app_bundle <- function(archive_name = "shinyapp.zip", dir_build = "_site") {
if (!app_dir_exists(dir_build)) {
build_app()
}
archive::archive_write_files(
archive = fs::path("app_bundle", archive_name),
files = c(
fs::dir_ls("_site", recurse = TRUE)
),
format = "zip"
)
invisible(TRUE)
}
extract_app_bundle <- function(archive_name = "shinyapp.zip", dir_build = "_site", overwrite = TRUE) {
if (isTRUE(overwrite) && fs::dir_exists(dir_build)) {
fs::dir_delete(dir_build)
}
archive::archive_extract(
archive = fs::path("app_bundle", archive_name),
dir = "."
)
invisible(TRUE)
}