Learn R Programming

bigstatsr (version 1.6.1)

FBM-class: Class FBM

Description

A reference class for storing and accessing matrix-like data stored in files on disk. This is very similar to Filebacked Big Matrices provided by the bigmemory package (see the corresponding vignette).

Convert a matrix (or a data frame) to an FBM.

Usage

FBM(
  nrow,
  ncol,
  type = c("double", "float", "integer", "unsigned short", "unsigned char", "raw"),
  init = NULL,
  backingfile = tempfile(tmpdir = getOption("FBM.dir")),
  create_bk = TRUE,
  is_read_only = FALSE
)

as_FBM( x, type = c("double", "float", "integer", "unsigned short", "unsigned char", "raw"), backingfile = tempfile(tmpdir = getOption("FBM.dir")), is_read_only = FALSE )

Arguments

nrow

Number of rows.

ncol

Number of columns.

type

Type of the Filebacked Big Matrix (default is double). Either

  • "double" (double precision -- 64 bits)

  • "float" (single precision -- 32 bits)

  • "integer"

  • "unsigned short": can store integer values from 0 to 65535. It has vocation to become the basis for a FBM.code65536.

  • "raw" or "unsigned char": can store integer values from 0 to 255. It is the basis for class FBM.code256 in order to access 256 arbitrary different numeric values. It is used in package bigsnpr.

init

Either a single value (e.g. 0) or as many value as the number of elements of the FBM. Default doesn't initialize the matrix.

backingfile

Path to the file storing the FBM data on disk. An extension ".bk" will be automatically added. Default stores in the temporary directory, which you can change using global option "FBM.dir".

create_bk

Whether to create a backingfile (the default) or use an existing one (which should be named by the backingfile parameter and have an extension ".bk"). For example, this could be used to convert a filebacked big.matrix from package bigmemory to a FBM (see the corresponding vignette).

is_read_only

Whether the FBM is read-only? Default is FALSE.

x

A matrix or an data frame (2-dimensional data).

Details

An object of class FBM has many fields:

  • $address: address of the external pointer containing the underlying C++ object for read-only mapping, to be used as a XPtr<FBM> in C++ code

  • $extptr: (internal) use $address instead

  • $address_rw: address of the external pointer containing the underlying C++ object for read/write mapping, to be used as a XPtr<FBM_RW> in C++ code

  • $extptr_rw: (internal) use $address_rw instead

  • $nrow: number of rows

  • $ncol: number of columns

  • $type: (internal) use type_size or type_chr instead

  • $type_chr: FBM type as character, e.g. "double"

  • $type_size: size of FBM type in bytes (e.g. "double" is 8 and "float" is 4)

  • $backingfile or $bk: File with extension 'bk' that stores the numeric data of the FBM

  • $rds: 'rds' file (that may not exist) corresponding to the 'bk' file

  • $is_saved: whether this object is stored in $rds?

  • $is_read_only: whether it is (not) allowed to modify data?

And some methods:

  • $save(): Save the FBM object in $rds. Returns the FBM.

  • add_columns(<ncol_add>): Add some columns to the FBM by appending the backingfile with some data. Returns the FBM invisibly.

  • $bm(): Get this object as a filebacked.big.matrix to be used by package {bigmemory}.

  • $bm.desc(): Get this object as a filebacked.big.matrix descriptor to be used by package {bigmemory}.

  • $check_write_permissions(): Error if the FBM is read-only.

See Also

big_attach big_copy

Examples

Run this code
mat <- matrix(1:4, 2)
X_from_mat <- as_FBM(mat)

## You can save this object in an .rds file to use it in another session
X_from_mat$is_saved
X_from_mat$save()
X_from_mat$is_saved
(rds <- X_from_mat$rds)
## Use big_attach() to load the FBM object in another session
X_from_mat <- big_attach(rds)

## Standard accessors
X <- FBM(10, 10)
typeof(X)
X[] <- rnorm(length(X))
X[, 1:6]
X[] <- 1:100
X[, 1]
X[1, ]  # not recommended for large matrices
X[, -1]
X[, c(TRUE, FALSE)]
X[cbind(1:10, 1:10)] <- NA_real_

X[]  # access as standard R matrix

X <- FBM(150, 5)
X[] <- iris   ## you can replace with a df (but factors -> integers)
X2 <- as_FBM(iris)
identical(X[], X2[])

Run the code above in your browser using DataLab