A reference class for storing and accessing up to 256 arbitrary different values using a Filebacked Big Matrix of type unsigned char. Compared to a Filebacked Big Matrix, it adds a slot code which is used as a lookup table of size 256.

FBM.code256(
  nrow,
  ncol,
  code = rep(NA_real_, 256),
  init = NULL,
  backingfile = tempfile(tmpdir = getOption("FBM.dir")),
  create_bk = TRUE,
  is_read_only = FALSE
)

add_code256(x, code)

Arguments

nrow

Number of rows.

ncol

Number of columns.

code

A numeric vector (of length 256). You should construct it with rep(NA_real_, 256) and then replace the values which are of interest to you.

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 FBM.

Examples

X <- FBM(10, 10, type = "raw")
X[] <- sample(as.raw(0:3), size = length(X), replace = TRUE)
X[]
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]   03   00   01   02   01   02   03   01   03    03
#>  [2,]   01   03   00   01   00   03   01   02   01    03
#>  [3,]   01   03   00   00   01   01   03   00   01    03
#>  [4,]   03   01   02   02   03   00   01   00   00    02
#>  [5,]   03   01   02   03   00   01   00   00   02    00
#>  [6,]   01   03   03   01   03   02   00   00   02    03
#>  [7,]   01   01   02   02   00   01   00   02   00    02
#>  [8,]   02   00   01   00   03   03   02   00   02    00
#>  [9,]   03   00   01   00   00   01   03   01   00    01
#> [10,]   00   00   02   03   00   01   02   00   03    02

# From an FBM of type 'raw' ('unsigned char')
code <- rep(NA_real_, 256)
code[1:3] <- c(1, 3, 5)

X.code <- add_code256(X, code)
X.code[]
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]   NA    1    3    5    3    5   NA    3   NA    NA
#>  [2,]    3   NA    1    3    1   NA    3    5    3    NA
#>  [3,]    3   NA    1    1    3    3   NA    1    3    NA
#>  [4,]   NA    3    5    5   NA    1    3    1    1     5
#>  [5,]   NA    3    5   NA    1    3    1    1    5     1
#>  [6,]    3   NA   NA    3   NA    5    1    1    5    NA
#>  [7,]    3    3    5    5    1    3    1    5    1     5
#>  [8,]    5    1    3    1   NA   NA    5    1    5     1
#>  [9,]   NA    1    3    1    1    3   NA    3    1     3
#> [10,]    1    1    5   NA    1    3    5    1   NA     5

# Or directly
X.code2 <- FBM.code256(10, 10, code, init = sample(as.raw(0:3), 100, TRUE))
X.code2[]
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    1    3    3   NA    3    5    1   NA    5     3
#>  [2,]    5    5    3   NA    3   NA    5    5    1    NA
#>  [3,]    1   NA    1    1   NA   NA    3    1    1     3
#>  [4,]    5   NA    1    5    1    5    1    1    3     1
#>  [5,]    5    1    3   NA   NA    1    3    5    5     3
#>  [6,]    5    5   NA   NA    1    1    5    5    5    NA
#>  [7,]    3    1    1   NA   NA    3    3    1    5     1
#>  [8,]   NA    3   NA    1   NA    1    1   NA    3    NA
#>  [9,]    3   NA   NA    3    5    3    5   NA    3     5
#> [10,]   NA   NA    1    5    3   NA   NA    3    1     3

# Get a new FBM.code256 object with another code (but same underlying data)
X.code3 <- X.code$copy(code = rnorm(256))
all.equal(X.code$code256, code)
#> [1] TRUE