This is the abstract base class for data backends.

Data Backends provide a layer of abstraction for various data storage systems. The required set of operations to implement is listed in the Methods section.

Note that all data access is handled transparently via the Task. It is not recommended to work directly with the DataBackend.

See DataBackendDataTable or DataBackendMatrix for exemplary implementations of this 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())
  • data :: any The format of the input data depends on the specialization. E.g., DataBackendDataTable expects a data.table::data.table() and DataBackendMatrix expects a Matrix::Matrix() constructed with the Matrix package.

  • primary_key :: character(1) Each DataBackend needs a way to address rows, which is done via a column of unique values, referenced here by primary_key. The use of this variable may differ between backends.

  • data_formats (character()) Set of supported formats, e.g. "data.table" or "Matrix".


  • 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 of these supported formats can be picked 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 format is "data.table". 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.

  • distinct(rows, cols) (integer() | character(), character()) -> named list() Returns a named list of vectors of distinct values for each column specified. Non-existing columns are silently ignored. If rows is NULL, all possible distinct values will be returned, even if they do not occur. This affects factor-like variables with empty levels.

  • 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

Other DataBackend: DataBackendDataTable, DataBackendMatrix, as_data_backend

  • DataBackend
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")
b$data(rows = 1:2, cols = "x")
b$distinct(rows = b$rownames, "y")
b$missings(rows = b$rownames, cols = names(data))
# }
Documentation reproduced from package mlr3, version 0.1.0-9000, License: MIT + file LICENSE

Community examples

Looks like there are no examples yet.