Skip to content

Commit

Permalink
wip: Refactor app.R
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilzyla committed Jan 29, 2024
1 parent 7d3662f commit cd6c019
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions R/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ configure_logger <- function() {
}
}

load_main_module <- function() {
source_main <- function() {
main <- new.env(parent = baseenv())
source(fs::path("app", "main.R"), local = main)
main
}

box_use_main <- function() {
# Silence "no visible binding" notes raised by `box::use()` on R CMD check.
app <- NULL
main <- NULL
Expand All @@ -55,6 +61,13 @@ as_top_level <- function(shiny_module) {
wrap(shiny_module)
}

as_shiny_app <- function(main) {
shiny::shinyApp(
ui = with_head_tags(main$ui),
server = main$server
)
}

with_head_tags <- function(ui) {
wrap <- function(tag) {
shiny::tagList(
Expand Down Expand Up @@ -126,20 +139,14 @@ app <- function() {

entrypoint <- read_config()$legacy_entrypoint
if (identical(entrypoint, "app_dir")) {
return(shiny::shinyAppDir("app"))
}

if (identical(entrypoint, "source")) {
main <- new.env()
source(fs::path("app", "main.R"), local = main)
shiny::shinyAppDir("app")
} else if (identical(entrypoint, "source")) {
as_shiny_app(source_main())
} else if (identical(entrypoint, "box_top_level")) {
as_shiny_app(box_use_main())
} else if (is.null(entrypoint)) {
as_shiny_app(as_top_level(box_use_main()))
} else {
main <- load_main_module()
if (!identical(entrypoint, "box_top_level")) {
main <- as_top_level(main)
}
stop()
}
shiny::shinyApp(
ui = with_head_tags(main$ui),
server = main$server
)
}

0 comments on commit cd6c019

Please sign in to comment.