forked from RConsortium/submissions-pilot4-webR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.R
86 lines (77 loc) · 2.5 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
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)
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()} 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),
"renv/.gitignore",
"renv/activate.R",
"renv/settings.json",
"utils.R",
"renv.lock",
"submissions-pilot4-webr.Rproj",
".Rprofile"
),
format = "zip"
)
invisible(TRUE)
}