This is the abstract base class for data backends. See DataBackendDataTable or DataBackendMatrix for exemplary implementations of this interface.

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.


R6::R6Class object.


DataBackend$new(data, primary_key = NULL)
  • 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 typically handled by a primary_key column of unique values. The use of this variable may differ between backends.


  • 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 a specific 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. Non-existing rows and columns 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.