This is the abstract base class for data backends.

Data Backends provide a layer of abstraction for various data storage systems. It is not recommended to work directly with the DataBackend. Instead, all data access is handled transparently via the Task.

To connect to out-of-memory database management systems such as SQL servers, see mlr3db.

The required set of fields and methods to implement a custom DataBackend is listed in the respective sections. See DataBackendDataTable or DataBackendMatrix for exemplary implementations of the interface.


R6::R6Class object.


Note: This object is typically constructed via a derived classes, e.g. DataBackendDataTable or DataBackendMatrix, or via the S3 method as_data_backend().

DataBackend$new(data, primary_key = NULL, data_formats = "data.table", converters = list())


  • nrow :: integer(1)
    Number of rows (observations).

  • ncol :: integer(1)
    Number of columns (variables), including the primary key column.

  • colnames :: character()
    Returns vector of all column names, including the primary key column.

  • rownames :: (integer() | character())
    Returns vector of all distinct row identifiers, i.e. the primary key column.

  • hash :: character(1)
    Returns a unique hash for this backend. This hash is cached.

  • data_formats :: character()
    Vector of supported data formats. A specific format can be chosen in the $data() method.


  • data(rows = NULL, cols = NULL, format = "data.table")
    (integer() | character(), character()) -> any
    Returns a slice of the data in the specified format. Currently, the only supported formats are "data.table" and "Matrix". The rows must be addressed as vector of primary key values, columns must be referred to via column names. Queries for rows with no matching row id and queries for columns with no matching column name are silently ignored. Rows are guaranteed to be returned in the same order as rows, columns may be returned in an arbitrary order. Duplicated row ids result in duplicated rows, duplicated column names lead to an exception.

  • distinct(rows, cols, na_rm = TRUE)
    (integer() | character(), character(), logical(1)) -> named list()
    Returns a named list of vectors of distinct values for each column specified. If na_rm is TRUE, missing values are removed from the returned vectors of distinct values. Non-existing rows and columns are silently ignored.

    If rows is NULL, all possible distinct values will be returned, even if the value is not present in the data. This affects factor-like variables with empty levels, if supported by the backend.

  • head(n = 6)
    integer(1) -> data.table::data.table()
    Returns the first up-to n rows of the data as data.table::data.table().

  • missings(rows, cols)
    (integer() | character(), character()) -> named integer()
    Returns the number of missing values per column in the specified slice of data. Non-existing rows and columns are silently ignored.

See also

Extension Packages: mlr3db

Other DataBackend: DataBackendDataTable, DataBackendMatrix, as_data_backend


data = data.table::data.table(id = 1:5, x = runif(5), y = sample(letters[1:3], 5, replace = TRUE)) b = DataBackendDataTable$new(data, primary_key = "id") print(b)
#> <DataBackendDataTable> (5x3) #> id x y #> 1 0.70066184 a #> 2 0.04373037 a #> 3 0.91952597 b #> 4 0.98109477 a #> 5 0.81599705 a
#> id x y #> 1: 1 0.70066184 a #> 2: 2 0.04373037 a
b$data(rows = 1:2, cols = "x")
#> x #> 1: 0.70066184 #> 2: 0.04373037
b$distinct(rows = b$rownames, "y")
#> $y #> [1] "a" "b" #>
b$missings(rows = b$rownames, cols = names(data))
#> id x y #> 0 0 0