FBM-class {bigstatsr} | R Documentation |
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
|
init |
Either a single value (e.g. |
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 |
is_read_only |
Whether the FBM is read-only? Default is |
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 aXPtr<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 aXPtr<FBM_RW>
in C++ code -
$extptr_rw
: (internal) use$address_rw
instead -
$nrow
: number of rows -
$ncol
: number of columns -
$type
: (internal) usetype_size
ortype_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 afilebacked.big.matrix
to be used by package {bigmemory}. -
$bm.desc()
: Get this object as afilebacked.big.matrix
descriptor to be used by package {bigmemory}. -
$check_write_permissions()
: Error if the FBM is read-only.
See Also
Examples
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[])