Skip to contents

Shiny apps built with R6 classes keep their modules in methods, not in top-level functions. A tree that only reads name <- function(...) definitions skips them entirely: app_ui() and app_server() show up as leaves, and every module call inside them is invisible. In this vignette I’ll cover how call_tree_dir() reads R6 classes, which method calls it can follow, and where it stops.

The example app

The app used throughout this vignette ships with stackcallr in inst/examples/r6app/. It has three R6 classes and a few helper functions:

%%{init: {'theme': 'base', 'themeVariables': { 'fontFamily': 'monospace', 'fontSize':'16px', 'darkMode':false}}}%%

flowchart TD
    launch(["<code>launch()</code>"])
    launch --> appui("<code>app_ui()</code>")
    launch --> appsrv("<code>app_server()</code>")

    subgraph classes["<strong>R6 classes</strong>"]
        inputs("<code>ModInputs</code>")
        plot("<code>ModPlot</code>")
        base("<code>ModBase</code>")
    end

    appui --> inputs
    appui --> plot
    appsrv --> inputs
    appsrv --> plot
    inputs -. "inherit" .-> base
    plot -. "inherit" .-> base

    subgraph helpers["<strong>Helpers</strong>"]
        sv[["<code>select_var()</code>"]]
        mp[["<code>make_plot()</code>"]]
        lm[["<code>log_msg()</code>"]]
    end

    inputs --> sv
    plot --> mp
    base --> lm

    style classes fill:none,stroke:#7FBFC6,stroke-width:1px,rx:10,ry:10
    style helpers fill:none,stroke:#7FBFC6,stroke-width:1px,rx:10,ry:10
    style sv fill:#E8A33D,color:#1B2A41,stroke:#E8A33D
    style mp fill:#E8A33D,color:#1B2A41,stroke:#E8A33D
    style lm fill:#E8A33D,color:#1B2A41,stroke:#E8A33D
    style launch fill:#1B2A41,color:#F2E6C2,stroke:#E8A33D,stroke-width:2px
    style appui fill:#2A6F77,color:#FFFFFF,stroke:#7FBFC6,rx:5,ry:5
    style appsrv fill:#2A6F77,color:#FFFFFF,stroke:#7FBFC6,rx:5,ry:5
    style inputs fill:#2A6F77,color:#FFFFFF,stroke:#7FBFC6,rx:5,ry:5
    style plot fill:#2A6F77,color:#FFFFFF,stroke:#7FBFC6,rx:5,ry:5
    style base fill:#2A6F77,color:#FFFFFF,stroke:#7FBFC6,rx:5,ry:5

The example app: functions, R6 classes, and helpers

ModBase holds what every module shares: initialize() stores the module id and its namespace function, and a private log() method writes a message.

ModBase <- R6::R6Class("ModBase",
  public = list(
    initialize = function(id) {
      private$id <- id
      private$ns <- shiny::NS(id)
    }
  ),
  private = list(
    id = NULL,
    ns = NULL,
    log = function(msg) log_msg(private$id, msg)
  )
)

ModInputs and ModPlot inherit from ModBase, so neither defines its own initialize(). Each adds a ui() and a server() method. ModInputs also has a private picker() method:

ModInputs <- R6::R6Class("ModInputs",
  inherit = ModBase,
  public = list(
    ui = function() private$picker(),
    server = function() {
      shiny::moduleServer(private$id, function(input, output, session) {
        private$log("inputs ready")
        shiny::reactive(input$var)
      })
    }
  ),
  private = list(
    picker = function() select_var(private$ns("var"))
  )
)
ModPlot <- R6::R6Class("ModPlot",
  inherit = ModBase,
  public = list(
    ui = function() shiny::plotOutput(private$ns("plot")),
    server = function(var) {
      shiny::moduleServer(private$id, function(input, output, session) {
        private$log("plot ready")
        output$plot <- shiny::renderPlot(make_plot(var()))
      })
    }
  )
)

app_ui() and app_server() create one object per module with $new() and call its methods:

app_ui <- function() {
  inputs <- ModInputs$new("inputs")
  plot <- ModPlot$new("plot")
  shiny::fluidPage(inputs$ui(), plot$ui())
}

app_server <- function(input, output, session) {
  inputs <- ModInputs$new("inputs")
  plot <- ModPlot$new("plot")
  var <- inputs$server()
  plot$server(var)
}

launch <- function() shiny::shinyApp(app_ui, app_server)

Methods are nodes

call_tree_dir() turns each method into a node named Class$method. initialize() is the exception: it’s named Class$new, because $new() is how it gets called. The tree from launch() follows the app all the way down to the helpers:

call_tree_dir(r6_app, root = "launch")
█─launch
├─█─app_ui
│ ├─█─ModInputs$new
│ │ └─ModBase$new
│ ├─█─ModPlot$new
│ │ └─ModBase$new
│ ├─█─ModInputs$ui
│ │ └─█─ModInputs$picker
│ │   └─select_var
│ └─ModPlot$ui
└─█─app_server
  ├─█─ModInputs$new
  │ └─ModBase$new
  ├─█─ModPlot$new
  │ └─ModBase$new
  ├─█─ModInputs$server
  │ └─█─ModBase$log
  │   └─log_msg
  └─█─ModPlot$server
    ├─█─ModBase$log
    │ └─log_msg
    └─make_plot

Three things in this tree come from the R6 rules rather than from plain function calls:

  • ModInputs$new calls ModBase$new. A class without its own initialize() gets a $new node that calls its parent’s $new.
  • ModInputs$ui calls ModInputs$picker, a private method. private$picker() resolves within the class.
  • ModInputs$server calls ModBase$log. private$log() isn’t defined in ModInputs, so the lookup moves up to ModBase, the class it inherits from.

The variable names in app_ui() don’t matter. inputs <- ModInputs$new("inputs") tells call_tree_dir() that inputs holds a ModInputs object, so inputs$ui() becomes ModInputs$ui.

Which calls resolve?

Every method call has the same shape: lhs$m(). call_tree_dir() decides what lhs refers to, then looks for m in that class. If the class doesn’t define m, it checks the parent named in inherit, then that class’s parent, and so on. Class$m names the first class that defines it:

%%{init: {'theme': 'base', 'themeVariables': { 'fontFamily': 'monospace', 'fontSize':'16px', 'darkMode':false}}}%%

flowchart TD
    entry(["<code>lhs$m()</code>"])
    entry --> isnew{"Is <code>lhs</code> a class<br>and <code>m</code> <code>new</code>?"}
    isnew -->|yes| newnode("<code>Class$new</code>")
    isnew -->|no| isself{"Is <code>lhs</code> <code>self</code>,<br><code>private</code>, or <code>super</code>?"}
    isself -->|yes| own("Start at the method's class<br>(its parent for <code>super</code>)")
    isself -->|no| isvar{"Was <code>lhs</code> assigned<br><code>Class$new()</code> here?"}
    isvar -->|yes| start("Start at <code>Class</code>")
    isvar -->|no| none(["Not an edge"])
    own --> found{"Is <code>m</code> defined in<br>this class or a parent?"}
    start --> found
    found -->|yes| method("<code>Class$m</code>")
    found -->|no| none

    style entry fill:#1B2A41,color:#F2E6C2,stroke:#E8A33D,stroke-width:2px
    style none fill:#B83A4B,color:#FFFFFF,stroke:#B83A4B
    style newnode fill:#2A6F77,color:#FFFFFF,stroke:#7FBFC6,rx:5,ry:5
    style method fill:#2A6F77,color:#FFFFFF,stroke:#7FBFC6,rx:5,ry:5
    style own fill:#F2E6C2,color:#1B2A41,stroke:#E4D8B8,rx:5,ry:5
    style start fill:#F2E6C2,color:#1B2A41,stroke:#E4D8B8,rx:5,ry:5
    style isnew fill:#F2E6C2,color:#1B2A41,stroke:#E4D8B8
    style isself fill:#F2E6C2,color:#1B2A41,stroke:#E4D8B8
    style isvar fill:#F2E6C2,color:#1B2A41,stroke:#E4D8B8
    style found fill:#F2E6C2,color:#1B2A41,stroke:#E4D8B8

How call_tree_dir() resolves lhs$m()

An instance variable counts when the function assigns it with <- or =, and the right-hand side contains Class$new(). That covers a wrapped call such as x <- tryCatch(Class$new(), error = function(e) NULL).

When none of the rules match, the call isn’t an edge. The name after $ is never read as a plain function call either. In result$summary, summary is a list element, so a summary() function defined in R/ doesn’t show up under it.

Methods as roots

A method can be the root of a tree. Write it the way it appears in the output, as "Class$method":

call_tree_dir(r6_app, root = "ModPlot$server")
█─ModPlot$server
├─█─ModBase$log
│ └─log_msg
└─make_plot

Use "Class$new" to start at a class’s initialize() method.

Modules with modules_only

modules_only = TRUE keeps the functions root calls directly, plus the Shiny modules below them. An R6 class counts as a module when any of its methods calls shiny::NS() or shiny::moduleServer(). Its $new and public methods are then kept, and private helpers such as picker() and log() are spliced out:

call_tree_dir(r6_app, root = "launch", modules_only = TRUE)
█─launch
├─█─app_ui
│ ├─█─ModInputs$new
│ │ └─ModBase$new
│ ├─█─ModPlot$new
│ │ └─ModBase$new
│ ├─ModInputs$ui
│ └─ModPlot$ui
└─█─app_server
  ├─█─ModInputs$new
  │ └─ModBase$new
  ├─█─ModPlot$new
  │ └─ModBase$new
  ├─ModInputs$server
  └─ModPlot$server

ModInputs and ModPlot count as modules because their server() methods call moduleServer(). ModBase counts too, because its initialize() calls NS(), so ModBase$new stays under each child’s $new.

What it can’t follow

call_tree_dir() reads the source without running it, so it only follows objects it can see being created. These calls aren’t edges:

  • An object stored in a field, such as self$data <- MovieData$new() followed by self$data$filter().
  • An object passed in as an argument, such as function(inputs) inputs$server().
  • An object returned by another function, such as x <- make_inputs() followed by x$ui().

The other tree builders don’t read R6 classes. call_tree_app() collects plain functions from the server’s environment, and call_stack() treats R6 dispatch as a leaf (see vignette("stackcallr")).

Recap

call_tree_dir() names each R6 method Class$method and shows initialize() as Class$new. It follows Class$new(), self$m(), private$m(), super$m(), and method calls on local variables assigned Class$new(), moving up through inherit when a method isn’t defined in the class itself. Methods can be roots, and a class that uses NS() or moduleServer() counts as a module for modules_only.

Learning more