Skip to content

Commit

Permalink
feat: Make auto-reload work with box_top_level entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilzyla committed Jan 31, 2024
1 parent fc4f6d8 commit 6469dd5
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions R/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,6 @@ configure_logger <- function() {
}
}

load_main_module <- function() {
# Silence "no visible binding" notes raised by `box::use()` on R CMD check.
app <- NULL
main <- NULL
box::use(app/main)
main
}

as_top_level <- function(app_env) {
list(
# Wrap the UI in a function to support Shiny bookmarking.
ui = function(request) app_env$main$ui("app"),
server = fix_server_reloading(function(input, output) {
app_env$main$server("app")
})
)
}

fix_server_reloading <- function(server) {
reparse(curly_wrap(server))
}
Expand Down Expand Up @@ -124,6 +106,24 @@ with_head_tags <- function(ui) {
}
}

call_ui <- function(ui, request) {
if (!is.function(ui)) {
ui
} else if (length(formals(ui)) == 0) {
ui()
} else {
ui(request)
}
}

call_server <- function(server, input, output, session) {
if ("session" %in% formalArgs(server)) {
server(input, output, session)
} else {
server(input, output)
}
}

#' Rhino application
#'
#' The entrypoint for a Rhino application.
Expand Down Expand Up @@ -170,7 +170,6 @@ with_head_tags <- function(ui) {
#' @export
app <- function() {
setup_box_path()
box::purge_cache()
configure_logger()
shiny::addResourcePath("static", fs::path_wd("app", "static"))

Expand All @@ -185,17 +184,27 @@ app <- function() {
server = main$server
)
} else if (identical(entrypoint, "box_top_level")) {
main <- load_main_module()
app_env <- load_app()
ui <- function(request) {
call_ui(app_env$main$ui, request)
}
server <- function(input, output, session) {
call_server(app_env$main$server, input, output, session)
}
shiny::shinyApp(
ui = with_head_tags(main$ui),
server = main$server
ui = with_head_tags(ui),
server = reparse(server)
)
} else if (is.null(entrypoint)) {
app_env <- load_app()
main <- as_top_level(app_env)
# Wrap the UI in a function to support Shiny bookmarking.
ui <- function(request) app_env$main$ui("app")
server <- function(input, output) {
app_env$main$server("app")
}
shiny::shinyApp(
ui = with_head_tags(main$ui),
server = main$server
ui = with_head_tags(ui),
server = fix_server_reloading(server)
)
} else {
stop()
Expand Down

0 comments on commit 6469dd5

Please sign in to comment.