# rstudio/dt architecture

> Architecture diagram of the GitHub repository rstudio/dt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rstudio/dt
- Repository: https://github.com/rstudio/dt

## Overview

This project is an R package that provides an interface to the DataTables JavaScript library. In essence, it lets users easily create interactive data tables (with rich features such as sorting, filtering, pagination, and extensions) from R. The package not only wraps the DataTables JS library using HTML widgets (for integration with web frameworks like Shiny) but also includes extensive examples, tests, documentation, and build configurations.

##### Main Components

a. R Package Core:
- Source code in the R/ directory (e.g., datatables.R, format.R, package.R, searchbuilder.R, shiny.R, utils.R) implements the main functionality.
- These functions generate the HTML widget configurations that integrate with DataTables.

b. HTML Widgets & Frontend Integration:
- The inst/htmlwidgets directory houses the JavaScript and CSS files needed for rendering the DataTables client-side. This includes integration files and third-party libraries (e.g., CSS files, JS files for DataTables extensions).

c. Examples & Demos:
- inst/examples contains various demo applications (e.g., DT-click, DT-edit, DT-filter, DT-searchExact, etc.) that showcase how to use different features (such as row editing, filtering, etc.). Some examples contain server.R and ui.R files meant for the Shiny framework.

d. Documentation & Vignettes:
- README.md, NEWS.md, and vignettes/DT.Rmd provide detailed documentation and usage examples.
- The man/ directory includes R documentation files (*.Rd) for the functions.

e. Testing & CI/CD:
- tests/ (with test files for various functionalities) for ensuring code quality.
- .github/workflows and codecov.yml files for continuous integration and test coverage tracking.

f. Package Metadata and Configuration:
- DESCRIPTION, NAMESPACE, LICENSE, and other configuration files that manage package build and distribution.

##### Relationships and Interactions

- The starting point is an R user session or a Shiny app which calls the main function datatable() in the package.
- The R functions from the R/ directory process the data and configuration parameters, then build an HTML widget that references the necessary JavaScript and CSS files.
- The HTML widget (constructed by the package code) calls on the DataTables JS functions provided in the inst/htmlwidgets directory.
- Depending on how the widget is used (standalone R session or integrated into a Shiny application), the resulting interactive table is rendered in a web browser.
- The examples (in inst/examples) and tests (in tests/) also highlight and validate these interactions.

##### Architectural Patterns and Design Principles

- Modular design: Each R file in the R/ directory encapsulates a distinct component (formatting, search, interaction with Shiny, etc.).
- Separation of concerns: Backend processing is handled by R functions while the frontend rendering is managed through HTML widgets and external JavaScript libraries.
- Reusability: Examples, tests, and vignettes are provided to allow users to learn and extend the package features.
- Integration with CI/CD: GitHub Actions are configured to run R CMD checks and test coverage tools ensure reliability.

##### Including Relevant Technologies, Frameworks, and Libraries

- R (with functions like datatable()) forms the backend logic of the package.
- HTML widgets facilitate the creation of interactive graphics/tables.
- The DataTables JavaScript library and its extensions (located in the inst/htmlwidgets/lib/ directories) deliver the client-side functionality.
- Shiny integration (as seen in sample apps in the inst/examples/* directories) allows interactive web applications.
- GitHub Actions and Codecov are employed for automation and testing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Interface"
        U("R User / Shiny App (calls datatable())"):::user
    end

    subgraph "Core R Functions"
        DC("datatable()"):::core
        F("format()"):::core
        P("package()"):::core
        SB("searchbuilder()"):::core
        S("shiny()"):::core
        UTL("utils()"):::core
    end

    subgraph "Frontend Integration"
        HW("HTML Widget Builder"):::frontend
        DTJS("DataTables JS Library"):::library
    end

    subgraph "Supporting Components"
        subgraph "Documentation & Vignettes"
            RD("README.md"):::support
            NM("NEWS.md"):::support
            VIG("DT Vignette"):::support
            MAN("Man Pages"):::support
        end
        subgraph "Examples & Demos"
            EX("Examples"):::support
        end
        subgraph "Testing & CI/CD"
            TST("Tests"):::support
            GH("GitHub Workflows"):::support
            CC("Codecov"):::support
        end
        subgraph "Package Metadata"
            DESC("DESCRIPTION"):::support
            NS("NAMESPACE"):::support
            LIC("LICENSE"):::support
        end
    end

    U -->|"calls"| DC
    DC -->|"builds"| HW
    HW -->|"renders"| DTJS

    %% Connections to Supporting Components
    DC -->|"docs"| RD
    DC -->|"example"| EX
    DC -->|"test"| TST
    DC -->|"config"| DESC

    %% Click Events for R Package Core
    click DC "https://github.com/rstudio/dt/blob/main/R/datatables.R"
    click F "https://github.com/rstudio/dt/blob/main/R/format.R"
    click P "https://github.com/rstudio/dt/blob/main/R/package.R"
    click SB "https://github.com/rstudio/dt/blob/main/R/searchbuilder.R"
    click S "https://github.com/rstudio/dt/blob/main/R/shiny.R"
    click UTL "https://github.com/rstudio/dt/blob/main/R/utils.R"

    %% Click Events for HTML Widgets & Frontend Integration
    click HW "https://github.com/rstudio/dt/tree/main/inst/htmlwidgets"
    click DTJS "https://github.com/rstudio/dt/tree/main/inst/htmlwidgets/lib"

    %% Click Event for Examples & Demos
    click EX "https://github.com/rstudio/dt/tree/main/inst/examples"

    %% Click Events for Documentation & Vignettes
    click RD "https://github.com/rstudio/dt/blob/main/README.md"
    click NM "https://github.com/rstudio/dt/blob/main/NEWS.md"
    click VIG "https://github.com/rstudio/dt/blob/main/vignettes/DT.Rmd"
    click MAN "https://github.com/rstudio/dt/tree/main/man"

    %% Click Events for Testing & CI/CD
    click TST "https://github.com/rstudio/dt/tree/main/tests"
    click GH "https://github.com/rstudio/dt/tree/main/.github/workflows"
    click CC "https://github.com/rstudio/dt/blob/main/codecov.yml"

    %% Click Events for Package Metadata
    click DESC "https://github.com/rstudio/dt/tree/main/DESCRIPTION"
    click NS "https://github.com/rstudio/dt/tree/main/NAMESPACE"
    click LIC "https://github.com/rstudio/dt/tree/main/LICENSE"

    classDef user fill:#FCECC9,stroke:#C17B00,stroke-width:2px
    classDef core fill:#CCF2FF,stroke:#609DBE,stroke-width:2px
    classDef frontend fill:#DFFFE0,stroke:#37A52B,stroke-width:2px
    classDef library fill:#FFF2CC,stroke:#E6B800,stroke-width:2px
    classDef support fill:#E8D5F0,stroke:#A76CC6,stroke-width:2px
```
