When writing a shiny app with many UI elements, keeping track of all the nesting gets tiresome and the code becomes less readable. Add to this the fact that many elements have many, many parameters and the problem gets worse. So much ({({({({({(})})})})})}).
I have been experimenting with what I call "radical flattening " to move as much of the code up to functions above the UI and server parts of the app. This minimizes nesting.
My question is, what do you all think of this design philosophy?
As an example, my leaf-peeping app (h/t Bob Rudis) looks like this:
# UI ---------------------------------------------------------------------------
ui <- fluidPage(
theme = bslib::bs_theme(preset = "darkly"),
titlePanel("U.S.A. Autumn Foliage 2023"),
h6("source: https://github.com/apsteinmetz/foliage.git"),
# top bar with a slider input for weeks
fluidRow(
column_slider(),
column_stage_pct("Not Yet"),
column_stage_pct("Peak"),
column_stage_pct("Too Late")
),
# plot of map
fluidRow(column(
width = 12, plotOutput("leafPlot", height = "800px")
))
)
# SERVER ----------------------------------------------------------------------
server <- function(input, output) {
output$week_label <- renderText({
as.character(weeks[input$cur_week])
})
output$early_pct <- renderText({
stage_pct(input$cur_week, "Too Early")
})
output$peak_pct <- renderText({
stage_pct(input$cur_week, "Peak")
})
output$late_pct <- renderText({
stage_pct(input$cur_week, "Too Late")
})
output$leafPlot <- renderPlot({
leaf_plot(input$cur_week)
})
}
Even code that is only called once is pushed out of the app logic.
You can see the full code on GitHub and the app itself here.