radical flattening of shiny app. Invitation to comment.

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.

2 Likes

Anytime I find myself having to make more than one attempt to debug any script, I start taking everything to functions that can be tested and verified independently so that I end up, ideally, being able to do

# main
f(x) |> g(x) |> h(x)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.