Skip to content

Commit c4eed32

Browse files
committed
refactor: Simplify app.R
1 parent 6469dd5 commit c4eed32

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
lines changed

R/app.R

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,17 @@ configure_logger <- function() {
3535
}
3636
}
3737

38-
fix_server_reloading <- function(server) {
39-
reparse(curly_wrap(server))
40-
}
41-
4238
reparse <- function(f) {
4339
eval(parse(text = deparse(f)), envir = environment(f))
4440
}
4541

46-
curly_wrap <- function(f) {
47-
wrapped <- function() {
48-
do.call(f, as.list(match.call())[-1])
49-
}
50-
formals(wrapped) <- formals(f)
51-
wrapped
42+
load_app_source <- function() {
43+
main <- new.env(parent = globalenv())
44+
source(fs::path("app", "main.R"), local = main)
45+
main
5246
}
5347

54-
load_app <- function() {
48+
load_app_box <- function() {
5549
# Silence "no visible binding" notes raised by `box::use()` on R CMD check.
5650
app <- NULL
5751
main <- NULL
@@ -88,39 +82,36 @@ register_reload_callback <- function(callback) {
8882
}
8983

9084
with_head_tags <- function(ui) {
91-
wrap <- function(tag) {
92-
shiny::tagList(
93-
shiny::tags$head(
94-
react_support(), # Needs to go before `app.min.js`, which defines the React components.
95-
shiny::tags$script(src = "static/js/app.min.js"),
96-
shiny::tags$link(rel = "stylesheet", href = "static/css/app.min.css", type = "text/css"),
97-
shiny::tags$link(rel = "icon", href = "static/favicon.ico", sizes = "any")
98-
),
99-
tag
100-
)
101-
}
102-
if (is.function(ui)) {
103-
purrr::compose(wrap, ui)
104-
} else {
105-
wrap(ui)
85+
head <- shiny::tags$head(
86+
react_support(), # Needs to go before `app.min.js`, which defines the React components.
87+
shiny::tags$script(src = "static/js/app.min.js"),
88+
shiny::tags$link(rel = "stylesheet", href = "static/css/app.min.css", type = "text/css"),
89+
shiny::tags$link(rel = "icon", href = "static/favicon.ico", sizes = "any")
90+
)
91+
function(request) {
92+
shiny::tagList(head, ui(request))
10693
}
10794
}
10895

109-
call_ui <- function(ui, request) {
96+
normalize_ui <- function(ui) {
11097
if (!is.function(ui)) {
111-
ui
98+
function(request) ui
11299
} else if (length(formals(ui)) == 0) {
113-
ui()
100+
function(request) ui()
114101
} else {
115-
ui(request)
102+
function(request) ui(request)
116103
}
117104
}
118105

119-
call_server <- function(server, input, output, session) {
106+
normalize_server <- function(server) {
120107
if ("session" %in% formalArgs(server)) {
121-
server(input, output, session)
108+
function(input, output, session) {
109+
server(input = input, output = output, session = session)
110+
}
122111
} else {
123-
server(input, output)
112+
function(input, output, session) {
113+
server(input = input, output = output)
114+
}
124115
}
125116
}
126117

@@ -177,34 +168,36 @@ app <- function() {
177168
if (identical(entrypoint, "app_dir")) {
178169
shiny::shinyAppDir("app")
179170
} else if (identical(entrypoint, "source")) {
180-
main <- new.env()
181-
source(fs::path("app", "main.R"), local = main)
171+
main <- load_app_source()
172+
ui <- normalize_ui(main$ui)
173+
server <- main$server
182174
shiny::shinyApp(
183-
ui = with_head_tags(main$ui),
175+
ui = with_head_tags(ui),
184176
server = main$server
185177
)
186178
} else if (identical(entrypoint, "box_top_level")) {
187-
app_env <- load_app()
179+
app_env <- load_app_box()
188180
ui <- function(request) {
189-
call_ui(app_env$main$ui, request)
181+
normalize_ui(app_env$main$ui)(request)
190182
}
191183
server <- function(input, output, session) {
192-
call_server(app_env$main$server, input, output, session)
184+
normalize_server(app_env$main$server)(input, output, session)
193185
}
194186
shiny::shinyApp(
195187
ui = with_head_tags(ui),
196188
server = reparse(server)
197189
)
198190
} else if (is.null(entrypoint)) {
199-
app_env <- load_app()
200-
# Wrap the UI in a function to support Shiny bookmarking.
201-
ui <- function(request) app_env$main$ui("app")
202-
server <- function(input, output) {
191+
app_env <- load_app_box()
192+
ui <- function(request) {
193+
app_env$main$ui("app")
194+
}
195+
server <- function(input, output, session) {
203196
app_env$main$server("app")
204197
}
205198
shiny::shinyApp(
206199
ui = with_head_tags(ui),
207-
server = fix_server_reloading(server)
200+
server = reparse(server)
208201
)
209202
} else {
210203
stop()

0 commit comments

Comments
 (0)