# Programming on data.table

### 2026-04-13

Translations of this document are available in: en | es | fr

## Introduction

`data.table`, from its very first releases, enabled the usage of `subset` and `with` (or `within`) functions by defining the `[.data.table` method. `subset` and `with` are base R functions that are useful for reducing repetition in code, enhancing readability, and reducing number the total characters the user has to type. This functionality is possible in R because of a quite unique feature called lazy evaluation. This feature allows a function to catch its arguments, before they are evaluated, and to evaluate them in a different scope than the one in which they were called. Let’s recap usage of the `subset` function.

    subset(iris, Species == "setosa")

Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species
--- | --- | --- | --- | ---
5.1 | 3.5 | 1.4 | 0.2 | setosa
4.9 | 3.0 | 1.4 | 0.2 | setosa
4.7 | 3.2 | 1.3 | 0.2 | setosa
4.6 | 3.1 | 1.5 | 0.2 | setosa
5.0 | 3.6 | 1.4 | 0.2 | setosa
⋮ | ⋮ | ⋮ | ⋮ | ⋮
4.8 | 3.0 | 1.4 | 0.3 | setosa
5.1 | 3.8 | 1.6 | 0.2 | setosa
4.6 | 3.2 | 1.4 | 0.2 | setosa
5.3 | 3.7 | 1.5 | 0.2 | setosa
5.0 | 3.3 | 1.4 | 0.2 | setosa

Here, `subset` takes the second argument and evaluates it within the scope of the `data.frame` given as its first argument. This removes the need for variable repetition, making it less prone to errors, and makes the code more readable.

## Problem description

The problem with this kind of interface is that we cannot easily parameterize the code that uses it. This is because the expressions passed to those functions are substituted before being evaluated.

### Example

    my_subset = function(data, col, val) {
      subset(data, col == val)
    }
    my_subset(iris, Species, "setosa")

    # Error: object 'Species' not found

### Approaches to the problem

There are multiple ways to work around this problem.

#### Avoid lazy evaluation

The easiest workaround is to avoid lazy evaluation in the first place, and fall back to less intuitive, more error-prone approaches like `df[["variable"]]`, etc.

    my_subset = function(data, col, val) {
      data[data[[col]] == val & !is.na(data[[col]]), ]
    }
    my_subset(iris, col = "Species", val = "setosa")

Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species
--- | --- | --- | --- | ---
5.1 | 3.5 | 1.4 | 0.2 | setosa
4.9 | 3.0 | 1.4 | 0.2 | setosa
4.7 | 3.2 | 1.3 | 0.2 | setosa
4.6 | 3.1 | 1.5 | 0.2 | setosa
5.0 | 3.6 | 1.4 | 0.2 | setosa
⋮ | ⋮ | ⋮ | ⋮ | ⋮
4.8 | 3.0 | 1.4 | 0.3 | setosa
5.1 | 3.8 | 1.6 | 0.2 | setosa
4.6 | 3.2 | 1.4 | 0.2 | setosa
5.3 | 3.7 | 1.5 | 0.2 | setosa
5.0 | 3.3 | 1.4 | 0.2 | setosa

Here, we compute a logical vector of length `nrow(iris)`, then this vector is supplied to the `i` argument of `[.data.frame` to perform ordinary “logical vector”-based subsetting. To align with `subset()`, which also drops NAs, we need to include an additional use of `data[[col]]` to catch that. It works well enough for this simple example, but it lacks flexibility, introduces variable repetition, and requires user to change the function interface to pass the column name as a character rather than unquoted symbol. The more complex the expression we need to parameterize, the less practical this approach becomes.

#### Use of `parse` / `eval`

This method is usually preferred by newcomers to R as it is, perhaps, the most straightforward conceptually. This way requires producing the required expression using string concatenation, parsing it, and then evaluating it.

    my_subset = function(data, col, val) {
      data = deparse(substitute(data))
      col  = deparse(substitute(col))
      val  = paste0("'", val, "'")
      text = paste0("subset(", data, ", ", col, " == ", val, ")")
      eval(parse(text = text)[[1L]])
    }
    my_subset(iris, Species, "setosa")

Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species
--- | --- | --- | --- | ---
5.1 | 3.5 | 1.4 | 0.2 | setosa
4.9 | 3.0 | 1.4 | 0.2 | setosa
4.7 | 3.2 | 1.3 | 0.2 | setosa
4.6 | 3.1 | 1.5 | 0.2 | setosa
5.0 | 3.6 | 1.4 | 0.2 | setosa
⋮ | ⋮ | ⋮ | ⋮ | ⋮
4.8 | 3.0 | 1.4 | 0.3 | setosa
5.1 | 3.8 | 1.6 | 0.2 | setosa
4.6 | 3.2 | 1.4 | 0.2 | setosa
5.3 | 3.7 | 1.5 | 0.2 | setosa
5.0 | 3.3 | 1.4 | 0.2 | setosa

We have to use `deparse(substitute(...))` to catch the actual names of objects passed to function, so we can construct the `subset` function call using those original names. Although this provides unlimited flexibility with relatively low complexity, use of `eval(parse(...))` should be avoided. The main reasons are:

- lack of syntax validation
- vulnerability to code injection
- the existence of better alternatives

Martin Machler, R Project Core Developer, once said:

> Sorry but I don’t understand why too many people even think a string was something that could be evaluated. You must change your mindset, really. Forget all connections between strings on one side and expressions, calls, evaluation on the other side. The (possibly) only connection is via `parse(text = ....)` and all good R programmers should know that this is rarely an efficient or safe means to construct expressions (or calls). Rather learn more about `substitute()`, `quote()`, and possibly the power of using `do.call(substitute, ......)`.

#### Computing on the language

The aforementioned functions, along with some others (including `as.call`, `as.name`/`as.symbol`, `bquote`, and `eval`), can be categorized as functions to compute on the language, as they operate on language objects (e.g. `call`, `name`/`symbol`).

    my_subset = function(data, col, val) {
      eval(substitute(subset(data, col == val)))
    }
    my_subset(iris, Species, "setosa")

Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species
--- | --- | --- | --- | ---
5.1 | 3.5 | 1.4 | 0.2 | setosa
4.9 | 3.0 | 1.4 | 0.2 | setosa
4.7 | 3.2 | 1.3 | 0.2 | setosa
4.6 | 3.1 | 1.5 | 0.2 | setosa
5.0 | 3.6 | 1.4 | 0.2 | setosa
⋮ | ⋮ | ⋮ | ⋮ | ⋮
4.8 | 3.0 | 1.4 | 0.3 | setosa
5.1 | 3.8 | 1.6 | 0.2 | setosa
4.6 | 3.2 | 1.4 | 0.2 | setosa
5.3 | 3.7 | 1.5 | 0.2 | setosa
5.0 | 3.3 | 1.4 | 0.2 | setosa

Here, we used the base R `substitute` function to transform the call `subset(data, col == val)` into `subset(iris, Species == "setosa")` by substituting `data`, `col`, and `val` with their original names (or values) from their parent environment. The benefits of this approach to the previous ones should be clear. Note that because we operate at the level of language objects, and don’t have to resort to string manipulation, we refer to this as computing on the language. There is a dedicated chapter on Computing on the language in R language manual. Although it is not necessary for programming on data.table, we encourage readers to read this chapter for the sake of better understanding this powerful and unique feature of R language.

#### Use third party packages

There are third party packages that can achieve what base R computing on the language routines do (`pryr`, `lazyeval` and `rlang`, to name a few).

Though these can be helpful, we will be discussing a `data.table`-unique approach here.

## Programming on data.table

Now that we’ve established the proper way to parameterize code that uses lazy evaluation, we can move on to the main subject of this vignette, programming on data.table.

Starting from version 1.15.0, data.table provides a robust mechanism for parameterizing expressions passed to the `i`, `j`, and `by` (or `keyby`) arguments of `[.data.table`. It is built upon the base R `substitute` function, and mimics its interface. Here, we introduce `substitute2` as a more robust and more user-friendly version of base R’s `substitute`. For a complete list of differences between `base::substitute` and `data.table::substitute2` please read the `substitute2` manual.

### Substituting variables and names

Let’s say we want to have a general function that applies a function to sum of two arguments that has been applied another function. As a concrete example, below we have a function to compute the length of the hypotenuse in a right triangle, knowing length of its legs.

\({\displaystyle c = \sqrt{a^2 + b^2}}\)

    square = function(x) x^2
    quote(
      sqrt(square(a) + square(b))
    )

    # sqrt(square(a) + square(b))

The goal is the make every name in the above call able to be passed as a parameter.

    substitute2(
      outer(inner(var1) + inner(var2)),
      env = list(
        outer = "sqrt",
        inner = "square",
        var1 = "a",
        var2 = "b"
      )
    )

    # sqrt(square(a) + square(b))

We can see in the output that both the functions names, as well as the names of the variables passed to those functions, have been replaced. We used `substitute2` for convenience. In this simple case, base R’s `substitute` could have been used as well, though it would’ve required usage of `lapply(env, as.name)`.

Now, to use substitution inside `[.data.table`, we don’t need to call the `substitute2` function. As it is now being used internally, all we have to do is to provide `env` argument, the same way as we’ve provided it to the `substitute2` function in the example above. Substitution can be applied to the `i`, `j` and `by` (or `keyby`) arguments of the `[.data.table` method. Note that setting the `verbose` argument to `TRUE` can be used to print expressions after substitution is applied. This is very useful for debugging.

Let’s use the `iris` data set as a demonstration. Just as an example, let’s pretend we want to compute the `Sepal.Hypotenuse`, treating the sepal width and length as if they were legs of a right triangle.

    DT = as.data.table(iris)

    str(
      DT[, outer(inner(var1) + inner(var2)),
         env = list(
           outer = "sqrt",
           inner = "square",
           var1 = "Sepal.Length",
           var2 = "Sepal.Width"
        )]
    )

    #  num [1:150] 6.19 5.75 5.69 5.55 6.16 ...

    # return as a data.table
    DT[, .(Species, var1, var2, out = outer(inner(var1) + inner(var2))),
       env = list(
         outer = "sqrt",
         inner = "square",
         var1 = "Sepal.Length",
         var2 = "Sepal.Width",
         out = "Sepal.Hypotenuse"
      )]

Species | Sepal.Length | Sepal.Width | Sepal.Hypotenuse
--- | --- | --- | ---
setosa | 5.1 | 3.5 | 6.185
setosa | 4.9 | 3.0 | 5.745
setosa | 4.7 | 3.2 | 5.686
setosa | 4.6 | 3.1 | 5.547
setosa | 5.0 | 3.6 | 6.161
⋮ | ⋮ | ⋮ | ⋮
virginica | 6.7 | 3.0 | 7.341
virginica | 6.3 | 2.5 | 6.778
virginica | 6.5 | 3.0 | 7.159
virginica | 6.2 | 3.4 | 7.071
virginica | 5.9 | 3.0 | 6.619

In the last call, we added another parameter, `out = "Sepal.Hypotenuse"`, that conveys the intended name of output column. Unlike base R’s `substitute`, `substitute2` will handle the substitution of the names of call arguments, as well.

Substitution works on `i` and `by` (or `keyby`), as well.

    DT[filter_col %in% filter_val,
       .(var1, var2, out = outer(inner(var1) + inner(var2))),
       by = by_col,
       env = list(
         outer = "sqrt",
         inner = "square",
         var1 = "Sepal.Length",
         var2 = "Sepal.Width",
         out = "Sepal.Hypotenuse",
         filter_col = "Species",
         filter_val = I(c("versicolor", "virginica")),
         by_col =  "Species"
      )]

Species | Sepal.Length | Sepal.Width | Sepal.Hypotenuse
--- | --- | --- | ---
versicolor | 7.0 | 3.2 | 7.697
versicolor | 6.4 | 3.2 | 7.155
versicolor | 6.9 | 3.1 | 7.564
versicolor | 5.5 | 2.3 | 5.962
versicolor | 6.5 | 2.8 | 7.077
⋮ | ⋮ | ⋮ | ⋮
virginica | 6.7 | 3.0 | 7.341
virginica | 6.3 | 2.5 | 6.778
virginica | 6.5 | 3.0 | 7.159
virginica | 6.2 | 3.4 | 7.071
virginica | 5.9 | 3.0 | 6.619

### Substitute functions

A tiny clarification may be useful on how we can substitute a function name in an expression. Note that providing `outer="sqrt"` (string) vs. `outer=sqrt` (symbol) is very different:

    DT[, outer(Sepal.Length), env = list(outer="sqrt"), verbose=TRUE]

    # Argument 'j' after substitute: sqrt(Sepal.Length)
    # Detected that j uses these columns: [Sepal.Length]
    #   [1] 2.258318 2.213594 2.167948 2.144761 2.236068 2.323790 2.144761 2.236068 2.097618 2.213594
    #  [11] 2.323790 2.190890 2.190890 2.073644 2.408319 2.387467 2.323790 2.258318 2.387467 2.258318
    #  [21] 2.323790 2.258318 2.144761 2.258318 2.190890 2.236068 2.236068 2.280351 2.280351 2.167948
    #  [31] 2.190890 2.323790 2.280351 2.345208 2.213594 2.236068 2.345208 2.213594 2.097618 2.258318
    #  [41] 2.236068 2.121320 2.097618 2.236068 2.258318 2.190890 2.258318 2.144761 2.302173 2.236068
    #  [51] 2.645751 2.529822 2.626785 2.345208 2.549510 2.387467 2.509980 2.213594 2.569047 2.280351
    #  [61] 2.236068 2.428992 2.449490 2.469818 2.366432 2.588436 2.366432 2.408319 2.489980 2.366432
    #  [71] 2.428992 2.469818 2.509980 2.469818 2.529822 2.569047 2.607681 2.588436 2.449490 2.387467
    #  [81] 2.345208 2.345208 2.408319 2.449490 2.323790 2.449490 2.588436 2.509980 2.366432 2.345208
    #  [91] 2.345208 2.469818 2.408319 2.236068 2.366432 2.387467 2.387467 2.489980 2.258318 2.387467
    # [101] 2.509980 2.408319 2.664583 2.509980 2.549510 2.756810 2.213594 2.701851 2.588436 2.683282
    # [111] 2.549510 2.529822 2.607681 2.387467 2.408319 2.529822 2.549510 2.774887 2.774887 2.449490
    # [121] 2.626785 2.366432 2.774887 2.509980 2.588436 2.683282 2.489980 2.469818 2.529822 2.683282
    # [131] 2.720294 2.810694 2.529822 2.509980 2.469818 2.774887 2.509980 2.529822 2.449490 2.626785
    # [141] 2.588436 2.626785 2.408319 2.607681 2.588436 2.588436 2.509980 2.549510 2.489980 2.428992

    #Argument 'j' after substitute: sqrt(Sepal.Length)
    ## DT[, sqrt(Sepal.Length)]

    DT[, outer(Sepal.Length), env = list(outer=sqrt), verbose=TRUE]

    # Argument 'j' after substitute: .Primitive("sqrt")(Sepal.Length)
    # Detected that j uses these columns: [Sepal.Length]
    #   [1] 2.258318 2.213594 2.167948 2.144761 2.236068 2.323790 2.144761 2.236068 2.097618 2.213594
    #  [11] 2.323790 2.190890 2.190890 2.073644 2.408319 2.387467 2.323790 2.258318 2.387467 2.258318
    #  [21] 2.323790 2.258318 2.144761 2.258318 2.190890 2.236068 2.236068 2.280351 2.280351 2.167948
    #  [31] 2.190890 2.323790 2.280351 2.345208 2.213594 2.236068 2.345208 2.213594 2.097618 2.258318
    #  [41] 2.236068 2.121320 2.097618 2.236068 2.258318 2.190890 2.258318 2.144761 2.302173 2.236068
    #  [51] 2.645751 2.529822 2.626785 2.345208 2.549510 2.387467 2.509980 2.213594 2.569047 2.280351
    #  [61] 2.236068 2.428992 2.449490 2.469818 2.366432 2.588436 2.366432 2.408319 2.489980 2.366432
    #  [71] 2.428992 2.469818 2.509980 2.469818 2.529822 2.569047 2.607681 2.588436 2.449490 2.387467
    #  [81] 2.345208 2.345208 2.408319 2.449490 2.323790 2.449490 2.588436 2.509980 2.366432 2.345208
    #  [91] 2.345208 2.469818 2.408319 2.236068 2.366432 2.387467 2.387467 2.489980 2.258318 2.387467
    # [101] 2.509980 2.408319 2.664583 2.509980 2.549510 2.756810 2.213594 2.701851 2.588436 2.683282
    # [111] 2.549510 2.529822 2.607681 2.387467 2.408319 2.529822 2.549510 2.774887 2.774887 2.449490
    # [121] 2.626785 2.366432 2.774887 2.509980 2.588436 2.683282 2.489980 2.469818 2.529822 2.683282
    # [131] 2.720294 2.810694 2.529822 2.509980 2.469818 2.774887 2.509980 2.529822 2.449490 2.626785
    # [141] 2.588436 2.626785 2.408319 2.607681 2.588436 2.588436 2.509980 2.549510 2.489980 2.428992

    #Argument 'j' after substitute: .Primitive("sqrt")(Sepal.Length)
    ## DT[, .Primitive("sqrt")(Sepal.Length)]

And while `.Primitive("sqrt")(Sepal.Length)` still works, it is almost never the desired form.

Even more importantly, if the symbol form is meant to be used, then it can, and should, be used directly in the expression, as there is no need for substitution.

    DT[, sqrt(Sepal.Length)]

    #   [1] 2.258318 2.213594 2.167948 2.144761 2.236068 2.323790 2.144761 2.236068 2.097618 2.213594
    #  [11] 2.323790 2.190890 2.190890 2.073644 2.408319 2.387467 2.323790 2.258318 2.387467 2.258318
    #  [21] 2.323790 2.258318 2.144761 2.258318 2.190890 2.236068 2.236068 2.280351 2.280351 2.167948
    #  [31] 2.190890 2.323790 2.280351 2.345208 2.213594 2.236068 2.345208 2.213594 2.097618 2.258318
    #  [41] 2.236068 2.121320 2.097618 2.236068 2.258318 2.190890 2.258318 2.144761 2.302173 2.236068
    #  [51] 2.645751 2.529822 2.626785 2.345208 2.549510 2.387467 2.509980 2.213594 2.569047 2.280351
    #  [61] 2.236068 2.428992 2.449490 2.469818 2.366432 2.588436 2.366432 2.408319 2.489980 2.366432
    #  [71] 2.428992 2.469818 2.509980 2.469818 2.529822 2.569047 2.607681 2.588436 2.449490 2.387467
    #  [81] 2.345208 2.345208 2.408319 2.449490 2.323790 2.449490 2.588436 2.509980 2.366432 2.345208
    #  [91] 2.345208 2.469818 2.408319 2.236068 2.366432 2.387467 2.387467 2.489980 2.258318 2.387467
    # [101] 2.509980 2.408319 2.664583 2.509980 2.549510 2.756810 2.213594 2.701851 2.588436 2.683282
    # [111] 2.549510 2.529822 2.607681 2.387467 2.408319 2.529822 2.549510 2.774887 2.774887 2.449490
    # [121] 2.626785 2.366432 2.774887 2.509980 2.588436 2.683282 2.489980 2.469818 2.529822 2.683282
    # [131] 2.720294 2.810694 2.529822 2.509980 2.469818 2.774887 2.509980 2.529822 2.449490 2.626785
    # [141] 2.588436 2.626785 2.408319 2.607681 2.588436 2.588436 2.509980 2.549510 2.489980 2.428992

If function name to be substituted needs to be namespace-qualified then namespace and function name can be substituted as any other symbol in the expression:

    DT[, ns::fun(Sepal.Length), env = list(ns="base", fun="sqrt"), verbose=TRUE]
    #Argument 'j' after substitute: base::sqrt(Sepal.Length)
    ## DT[, base::sqrt(Sepal.Length)]

### Substitute variables and character values

In the above example, we have seen a convenient feature of `substitute2`: automatic conversion from strings into names/symbols. An obvious question arises: what if we actually want to substitute a parameter with a character value, so as to have base R `substitute` behaviour. We provide a mechanism to escape automatic conversion by wrapping the elements into base R `I()` call. The `I` function marks an object as AsIs, preventing its arguments from character-to-symbol automatic conversion. (Read the `?AsIs` documentation for more details.) If base R behaviour is desired for the whole `env` argument, then it’s best to wrap the whole argument in `I()`. Alternatively, each list element can be wrapped in `I()` individually. Let’s explore both cases below.

    substitute(    # base R behaviour
      rank(input, ties.method = ties),
      env = list(input = as.name("Sepal.Width"), ties = "first")
    )

    # rank(Sepal.Width, ties.method = "first")

    substitute2(   # mimicking base R's "substitute" using "I"
      rank(input, ties.method = ties),
      env = I(list(input = as.name("Sepal.Width"), ties = "first"))
    )

    # rank(Sepal.Width, ties.method = "first")

    substitute2(   # only particular elements of env are used "AsIs"
      rank(input, ties.method = ties),
      env = list(input = "Sepal.Width", ties = I("first"))
    )

    # rank(Sepal.Width, ties.method = "first")

Note that conversion works recursively on each list element, including the escape mechanism of course.

    substitute2(   # all are symbols
      f(v1, v2),
      list(v1 = "a", v2 = list("b", list("c", "d")))
    )

    # f(a, list(b, list(c, d)))

    substitute2(   # 'a' and 'd' should stay as character
      f(v1, v2),
      list(v1 = I("a"), v2 = list("b", list("c", I("d"))))
    )

    # f("a", list(b, list(c, "d")))

### Substituting lists of arbitrary length

The example presented above illustrates a neat and powerful way to make your code more dynamic. However, there are many other much more complex cases that a developer might have to deal with. One common problem handling a list of arguments of arbitrary length.

An obvious use case could be to mimic `.SD` functionality by injecting a `list` call into the `j` argument.

    cols = c("Sepal.Length", "Sepal.Width")
    DT[, .SD, .SDcols = cols]

Sepal.Length | Sepal.Width
--- | ---
5.1 | 3.5
4.9 | 3.0
4.7 | 3.2
4.6 | 3.1
5.0 | 3.6
⋮ | ⋮
6.7 | 3.0
6.3 | 2.5
6.5 | 3.0
6.2 | 3.4
5.9 | 3.0

Having `cols` parameter, we’d want to splice it into a `list` call, making `j` argument look like in the code below.

    DT[, list(Sepal.Length, Sepal.Width)]

Sepal.Length | Sepal.Width
--- | ---
5.1 | 3.5
4.9 | 3.0
4.7 | 3.2
4.6 | 3.1
5.0 | 3.6
⋮ | ⋮
6.7 | 3.0
6.3 | 2.5
6.5 | 3.0
6.2 | 3.4
5.9 | 3.0

Splicing is an operation where a list of objects have to be inlined into an expression as a sequence of arguments to call. In base R, splicing `cols` into a `list` can be achieved using `as.call(c(quote(list), lapply(cols, as.name)))`. Additionally, starting from R 4.0.0, there is new interface for such an operation in the `bquote` function.

In data.table, we make it easier by automatically enlist-ing a list of objects into a list call with those objects. This means that any `list` object inside the `env` list argument will be turned into list `call`, making the API for that use case as simple as presented below.

    # this works
    DT[, j,
       env = list(j = as.list(cols)),
       verbose = TRUE]

    # Argument 'j' after substitute: list(Sepal.Length, Sepal.Width)
    # Detected that j uses these columns: [Sepal.Length, Sepal.Width]

Sepal.Length | Sepal.Width
--- | ---
5.1 | 3.5
4.9 | 3.0
4.7 | 3.2
4.6 | 3.1
5.0 | 3.6
⋮ | ⋮
6.7 | 3.0
6.3 | 2.5
6.5 | 3.0
6.2 | 3.4
5.9 | 3.0

    # this will not work
    #DT[, list(cols),
    #   env = list(cols = cols)]

It is important to provide a call to `as.list`, rather than simply a list, inside the `env` list argument, as is shown in the above example.

Let’s explore enlist-ing in more detail.

    DT[, j,  # data.table automatically enlists nested lists into list calls
       env = list(j = as.list(cols)),
       verbose = TRUE]

    # Argument 'j' after substitute: list(Sepal.Length, Sepal.Width)
    # Detected that j uses these columns: [Sepal.Length, Sepal.Width]

Sepal.Length | Sepal.Width
--- | ---
5.1 | 3.5
4.9 | 3.0
4.7 | 3.2
4.6 | 3.1
5.0 | 3.6
⋮ | ⋮
6.7 | 3.0
6.3 | 2.5
6.5 | 3.0
6.2 | 3.4
5.9 | 3.0

    DT[, j,  # turning the above 'j' list into a list call
       env = list(j = quote(list(Sepal.Length, Sepal.Width))),
       verbose = TRUE]

    # Argument 'j' after substitute: list(Sepal.Length, Sepal.Width)
    # Detected that j uses these columns: [Sepal.Length, Sepal.Width]

Sepal.Length | Sepal.Width
--- | ---
5.1 | 3.5
4.9 | 3.0
4.7 | 3.2
4.6 | 3.1
5.0 | 3.6
⋮ | ⋮
6.7 | 3.0
6.3 | 2.5
6.5 | 3.0
6.2 | 3.4
5.9 | 3.0

    DT[, j,  # the same as above but accepts character vector
       env = list(j = as.call(c(quote(list), lapply(cols, as.name)))),
       verbose = TRUE]

    # Argument 'j' after substitute: list(Sepal.Length, Sepal.Width)
    # Detected that j uses these columns: [Sepal.Length, Sepal.Width]

Sepal.Length | Sepal.Width
--- | ---
5.1 | 3.5
4.9 | 3.0
4.7 | 3.2
4.6 | 3.1
5.0 | 3.6
⋮ | ⋮
6.7 | 3.0
6.3 | 2.5
6.5 | 3.0
6.2 | 3.4
5.9 | 3.0

Now let’s try to pass a list of symbols, rather than list call to those symbols. We’ll use `I()` to escape automatic enlist-ing but, as this will also turn off character to symbol conversion, we also have to use `as.name`.

    DT[, j,  # list of symbols
       env = I(list(j = lapply(cols, as.name))),
       verbose = TRUE]

    # Error in `[.data.table`(DT, , j, env = I(list(j = lapply(cols, as.name))), : When with=FALSE, j-argument should be of type logical/character/integer indicating the columns to select.

    # Argument 'j' after substitute: list(Sepal.Length, Sepal.Width)

    DT[, j,  # again the proper way, enlist list to list call automatically
       env = list(j = as.list(cols)),
       verbose = TRUE]

    # Argument 'j' after substitute: list(Sepal.Length, Sepal.Width)
    # Detected that j uses these columns: [Sepal.Length, Sepal.Width]

Sepal.Length | Sepal.Width
--- | ---
5.1 | 3.5
4.9 | 3.0
4.7 | 3.2
4.6 | 3.1
5.0 | 3.6
⋮ | ⋮
6.7 | 3.0
6.3 | 2.5
6.5 | 3.0
6.2 | 3.4
5.9 | 3.0

Note that both expressions, although visually appearing to be the same, are not identical.

    str(substitute2(j, env = I(list(j = lapply(cols, as.name)))))

    # List of 2
    #  $ : symbol Sepal.Length
    #  $ : symbol Sepal.Width

    str(substitute2(j, env = list(j = as.list(cols))))

    #  language list(Sepal.Length, Sepal.Width)

For more detailed explanation on that matter, please see the examples in the `substitute2` documentation.

### Substitution of a complex query

Let’s take, as an example of a more complex function, calculating root mean square.

\({\displaystyle x_{\text{RMS}}={\sqrt{{\frac{1}{n}}\left(x_{1}^{2}+x_{2}^{2}+\cdots +x_{n}^{2}\right)}}}\)

It takes arbitrary number of variables on input, but now we cannot just splice a list of arguments into a list call because each of those arguments have to be wrapped in a `square` call. In this case, we have to splice by hand rather than relying on data.table’s automatic enlist.

First, we have to construct calls to the `square` function for each of the variables (see `inner_calls`). Then, we have to reduce the list of calls into a single call, having a nested sequence of `+` calls (see `add_calls`). Lastly, we have to substitute the constructed call into the surrounding expression (see `rms`).

    outer = "sqrt"
    inner = "square"
    vars = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")

    syms = lapply(vars, as.name)
    to_inner_call = function(var, fun) call(fun, var)
    inner_calls = lapply(syms, to_inner_call, inner)
    print(inner_calls)

    # [[1]]
    # square(Sepal.Length)
    #
    # [[2]]
    # square(Sepal.Width)
    #
    # [[3]]
    # square(Petal.Length)
    #
    # [[4]]
    # square(Petal.Width)
    #

    to_add_call = function(x, y) call("+", x, y)
    add_calls = Reduce(to_add_call, inner_calls)
    print(add_calls)

    # square(Sepal.Length) + square(Sepal.Width) + square(Petal.Length) +
    #     square(Petal.Width)

    rms = substitute2(
      expr = outer((add_calls) / len),
      env = list(
        outer = outer,
        add_calls = add_calls,
        len = length(vars)
      )
    )
    print(rms)

    # sqrt((square(Sepal.Length) + square(Sepal.Width) + square(Petal.Length) +
    #     square(Petal.Width))/4L)

    str(
      DT[, j, env = list(j = rms)]
    )

    #  num [1:150] 3.17 2.96 2.92 2.87 3.16 ...

    # same, but skipping last substitute2 call and using add_calls directly
    str(
      DT[, outer((add_calls) / len),
         env = list(
           outer = outer,
           add_calls = add_calls,
           len = length(vars)
        )]
    )

    #  num [1:150] 3.17 2.96 2.92 2.87 3.16 ...

    # return as data.table
    j = substitute2(j, list(j = as.list(setNames(nm = c(vars, "Species", "rms")))))
    j[["rms"]] = rms
    print(j)

    # list(Sepal.Length = Sepal.Length, Sepal.Width = Sepal.Width,
    #     Petal.Length = Petal.Length, Petal.Width = Petal.Width, Species = Species,
    #     rms = sqrt((square(Sepal.Length) + square(Sepal.Width) +
    #         square(Petal.Length) + square(Petal.Width))/4L))

    DT[, j, env = list(j = j)]

Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | rms
--- | --- | --- | --- | --- | ---
5.1 | 3.5 | 1.4 | 0.2 | setosa | 3.173
4.9 | 3.0 | 1.4 | 0.2 | setosa | 2.958
4.7 | 3.2 | 1.3 | 0.2 | setosa | 2.918
4.6 | 3.1 | 1.5 | 0.2 | setosa | 2.875
5.0 | 3.6 | 1.4 | 0.2 | setosa | 3.161
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮
6.7 | 3.0 | 5.2 | 2.3 | virginica | 4.643
6.3 | 2.5 | 5.0 | 1.9 | virginica | 4.317
6.5 | 3.0 | 5.2 | 2.0 | virginica | 4.536
6.2 | 3.4 | 5.4 | 2.3 | virginica | 4.595
5.9 | 3.0 | 5.1 | 1.8 | virginica | 4.274

    # alternatively
    j = as.call(c(
      quote(list),
      lapply(setNames(nm = vars), as.name),
      list(Species = as.name("Species")),
      list(rms = rms)
    ))
    print(j)

    # list(Sepal.Length = Sepal.Length, Sepal.Width = Sepal.Width,
    #     Petal.Length = Petal.Length, Petal.Width = Petal.Width, Species = Species,
    #     rms = sqrt((square(Sepal.Length) + square(Sepal.Width) +
    #         square(Petal.Length) + square(Petal.Width))/4L))

    DT[, j, env = list(j = j)]

Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | rms
--- | --- | --- | --- | --- | ---
5.1 | 3.5 | 1.4 | 0.2 | setosa | 3.173
4.9 | 3.0 | 1.4 | 0.2 | setosa | 2.958
4.7 | 3.2 | 1.3 | 0.2 | setosa | 2.918
4.6 | 3.1 | 1.5 | 0.2 | setosa | 2.875
5.0 | 3.6 | 1.4 | 0.2 | setosa | 3.161
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮
6.7 | 3.0 | 5.2 | 2.3 | virginica | 4.643
6.3 | 2.5 | 5.0 | 1.9 | virginica | 4.317
6.5 | 3.0 | 5.2 | 2.0 | virginica | 4.536
6.2 | 3.4 | 5.4 | 2.3 | virginica | 4.595
5.9 | 3.0 | 5.1 | 1.8 | virginica | 4.274

### Common mistakes

It is important to understand the difference between passing an object and a name that points to an object. See the verbose output of following examples.

    DT[, fun(Petal.Width), env = list(fun = mean), verbose=TRUE]

    # Argument 'j' after substitute: (function (x, ...)
    # UseMethod("mean"))(Petal.Width)
    # Detected that j uses these columns: [Petal.Width]
    # [1] 1.199333

    DT[, fun(Petal.Width), env = list(fun = "mean"), verbose=TRUE]

    # Argument 'j' after substitute: mean(Petal.Width)
    # Detected that j uses these columns: [Petal.Width]
    # [1] 1.199333

Users will usually want to substitute the function name rather than inserting the actual function body. Therefore, the second approach (passing a character string) is often preferred.

In case of any doubts on the `env` interface functioning, set `verbose = TRUE` to inspect how expressions are resolved internally.

### Use `env` argument from inside another function

It was a design decision that `env` argument will follow Standard Evaluation (SE) rules, i.e., values passed to `env` are evaluated in their original scope as-is. For more info on the topic see R Language manual: Computing on the language). As a result, using `env` argument from a function does not require any special handling. It also means that the `.()` alias for a `list()`, a la `env = .(.col="Petal.Length")`, will not work; use `env = list(.col="Petal.Length")` instead.

    fun = function(x, col.mean) {
      stopifnot(is.character(col.mean), is.data.table(x))
      x[, .(col_avg = mean(.col)), env = list(.col = col.mean)]
    }
    fun(DT, col.mean="Petal.Length")

col_avg
3.758

If the outer function itself follows NSE (Non-Standard Evaluation) rules, then it has to resolve language objects the same way as when passing its arguments to any other SE function.

    fun = function(x, col.mean) {
      col.mean = substitute(col.mean)
      stopifnot(is.name(col.mean), is.data.table(x))
      x[, .(col_avg = mean(.col)), env = list(.col = col.mean)]
    }
    fun(DT, col.mean=Petal.Length)

col_avg
3.758

## Retired interfaces

In `[.data.table`, it is also possible to use other mechanisms for variable substitution or for passing quoted expressions. These include `get` and `mget` for inline injection of variables by providing their names as strings, and `eval` that tells `[.data.table` that the expression we passed into an argument is a quoted expression and that it should be handled differently. Those interfaces should now be considered retired and we recommend using the new `env` argument, instead.

### `get`

    v1 = "Petal.Width"
    v2 = "Sepal.Width"

    DT[, .(total = sum(get(v1), get(v2)))]

total
638.5

    DT[, .(total = sum(v1, v2)),
       env = list(v1 = v1, v2 = v2)]

total
638.5

### `mget`

    v = c("Petal.Width", "Sepal.Width")

    DT[, lapply(mget(v), mean)]

Petal.Width | Sepal.Width
--- | ---
1.199 | 3.057

    DT[, lapply(v, mean),
       env = list(v = as.list(v))]

V1 | V2
--- | ---
1.199 | 3.057

    DT[, lapply(v, mean),
       env = list(v = as.list(setNames(nm = v)))]

Petal.Width | Sepal.Width
--- | ---
1.199 | 3.057

### `eval`

Instead of using `eval` function we can provide quoted expression into the element of `env` argument, no extra `eval` call is needed then.

    cl = quote(
      .(Petal.Width = mean(Petal.Width), Sepal.Width = mean(Sepal.Width))
    )

    DT[, eval(cl)]

Petal.Width | Sepal.Width
--- | ---
1.199 | 3.057

    DT[, cl, env = list(cl = cl)]

Petal.Width | Sepal.Width
--- | ---
1.199 | 3.057