# shcaba/ss-dl-tool architecture

> Architecture diagram of the GitHub repository shcaba/ss-dl-tool, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shcaba/ss-dl-tool
- Repository: https://github.com/shcaba/ss-dl-tool

## Overview

##### Project Type and Purpose
- This is a full-stack R/Shiny application (“The Stock Assessment Continuum Tool”) whose UI (ui.r + www/) and server logic (server.r, global.r, Functions.r) run inside an R session (optionally inside the provided devcontainer).
- Its purpose is to build Stock Synthesis (SS3) input files, call SS3 executables, parse SS3 outputs with the r4ss package, and present interactive plots, tables, and bookmarked scenarios to fisheries scientists via a web interface.

##### Top-Level File Structure and Architectural Patterns
- .devcontainer/ – Docker dev environment (VS Code Remote)
- ui.r, server.r, global.r, Functions.r – Shiny’s MVC pattern:
- global.r loads libraries & shared data
- ui.r defines inputs, panels and placeholder outputs
- server.r + Functions.r contain reactive logic, file I/O, process-spawning
- Example data files/, SSS_files/, SS_LB_files/, SS_LO_F_files/ – file-system “data store”
- Scenarios/ – runtime folders for user model runs (SS3 input, output, bookmarks, plots, tables)
- www/ – static assets (HTML docs, CSS, Phasing/PDFs) served by Shiny
- rsconnect/ – deployment metadata for shinyapps.io

##### Core Components & Interactions
a. Frontend (Shiny UI)
- Elements in ui.r + www/ (HTML/CSS assets) present controls (data upload, sliders, buttons) and view panels for plots/tables.
b. Backend (Shiny Server logic)
- Reactive handlers in server.r orchestrate workflows:
1. Read user inputs or example data (.csv/.rds)
2. Call SS3 executables (spawn process in SSS_files/ or SS_LB_files/) with generated control/dat files
3. Monitor completion, read Report.sso and scenario_input/input.rds
4. Use r4ss to parse outputs and generate ggplot2/flextable results
5. Write plots/tables to Scenarios/<run>/plots and Scenarios/<run>/tables
6. Feed results back to the UI for rendering (HTMLwidgets or images)
c. External Services & Tools
- SS3 (C++ executables) invoked as child processes
- r4ss package to parse SS3 results
- Optional parallel/future backends for concurrent runs
- shinyapps.io or local Shiny Server for hosting

##### Architectural Patterns & Principles
- MVC separation: global (model), server (controller), ui (view)
- File-system oriented persistence: no database; directories per scenario + bookmarking via .rds
- Process-isolation: each SS3 run in its own subdirectory under Scenarios
- Extensibility: SS3 configuration templates in SSS_files and example data in Example data files
- Containerization support via .devcontainer for reproducible environments

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Zone
    subgraph "User / Browser"
        Browser["User Browser"]:::deploy
    end

    %% Deployment Environments
    subgraph "Deployment Environments"
        DevContainer["Docker (.devcontainer/)"]:::deploy
        ShinyApps["shinyapps.io (rsconnect/)"]:::deploy
    end

    %% Shiny Server Process with MVC Layers
    subgraph "Shiny Server (R Process)"
        direction TB
        subgraph "View Layer"
            UI["Shiny UI (ui.r + www/)"]:::code
        end
        subgraph "Controller Layer"
            ServerMain["Server Logic (server.r)"]:::code
            Functions["Helper Module (Functions.r)"]:::code
        end
        subgraph "Model Layer"
            Global["Shared Model (global.r)"]:::code
        end
    end

    %% File System Data Stores
    subgraph "File System"
        direction TB
        ExampleData["Example Data files/"]:::data
        SSS["SSS_files/"]:::data
        SS_LB["SS_LB_files/"]:::data
        SS_LO_F["SS_LO_F_files/"]:::data
        Scenarios["Scenarios/"]:::data
        Bookmarks["shiny_bookmarks/input/input.rds"]:::data
        PrevSS["Previous SS versions/"]:::data
    end

    %% External Executables and Libraries
    subgraph "External Executables & Libraries"
        SS3["SS3 Executables"]:::external
        r4ss["r4ss Package"]:::external
        Parallel["parallel/future Backend"]:::external
    end

    %% Interactions
    Browser -->|"HTTP Request"| UI
    UI -->|"user inputs/events"| ServerMain
    ServerMain -->|"calls helpers"| Functions
    Global -->|"provides data & libs"| ServerMain
    ServerMain -->|"read/write"| ExampleData
    ServerMain -->|"load templates"| SSS
    ServerMain -->|"load templates"| SS_LB
    ServerMain -->|"load templates"| SS_LO_F
    ServerMain -->|"spawn process"| SS3
    SS3 -->|"outputs Report.sso"| Scenarios
    ServerMain -->|"invoke parse"| r4ss
    r4ss -->|"generate plots/tables"| Scenarios
    Scenarios -->|"serve results"| UI
    Parallel -->|"optional concurrency"| ServerMain
    DevContainer -->|"hosts"| Browser
    DevContainer -->|"runs"| UI
    ShinyApps -->|"hosts"| UI

    %% Click Events
    click UI "https://github.com/shcaba/ss-dl-tool/blob/master/ui.r"
    click UI "https://github.com/shcaba/ss-dl-tool/tree/master/www/"
    click Global "https://github.com/shcaba/ss-dl-tool/blob/master/global.r"
    click ServerMain "https://github.com/shcaba/ss-dl-tool/blob/master/server.r"
    click Functions "https://github.com/shcaba/ss-dl-tool/blob/master/Functions.r"
    click ExampleData "https://github.com/shcaba/ss-dl-tool/tree/master/Example data files/"
    click SSS "https://github.com/shcaba/ss-dl-tool/tree/master/SSS_files/"
    click SS_LB "https://github.com/shcaba/ss-dl-tool/tree/master/SS_LB_files/"
    click SS_LO_F "https://github.com/shcaba/ss-dl-tool/tree/master/SS_LO_F_files/"
    click Scenarios "https://github.com/shcaba/ss-dl-tool/tree/master/Scenarios/"
    click Bookmarks "https://github.com/shcaba/ss-dl-tool/blob/master/shiny_bookmarks/input/input.rds"
    click PrevSS "https://github.com/shcaba/ss-dl-tool/tree/master/Previous SS versions/"
    click DevContainer "https://github.com/shcaba/ss-dl-tool/tree/master/.devcontainer/"
    click ShinyApps "https://github.com/shcaba/ss-dl-tool/tree/master/rsconnect/"

    %% Styles
    classDef code fill:#c6f6d5,stroke:#2f8f2f,color:#064;
    classDef data fill:#d5ecf6,stroke:#2980b9,color:#045;
    classDef external fill:#fce5c6,stroke:#b35900,color:#b33500;
    classDef deploy fill:#f0d5f6,stroke:#6f2f8f,color:#420564;
```
