# NOT RUN {
path <- tempfile()
# ---------------- case 1: Create new array ------------------
arr <- lazyarray(path, storage_format = 'double', dim = c(2,3,4),
meta_name = 'lazyarray.meta')
arr[] <- 1:24
# Subset and get the first partition
arr[,,1]
# Partition file path (total 4 partitions)
arr$get_partition_fpath()
# Removing array doesn't clear the data
rm(arr); gc()
# ---------------- Case 2: Load from existing directory ----------------
## Important!!! Run case 1 first
# Load from existing path, no need to specify other params
arr <- lazyarray(path, meta_name = 'lazyarray.meta', read_only = TRUE)
arr[,,1]
# ---------------- Case 3: Import from existing data ----------------
## Important!!! Run case 1 first
# path exists, but meta is missing, all other params are required
# Notice the partition count increased from 4 to 5, and storage type converts
# from double to character
arr <- lazyarray(path = path, meta_name = 'lazyarray-character.meta',
file_names = c(1,2,3,4,'additional'),
storage_format = 'character', dim = c(2,3,5),
quiet = TRUE, read_only = FALSE)
# partition names
arr$get_partition_fpath(1:4, full_path = FALSE)
arr$get_partition_fpath(5, full_path = FALSE)
# The first dimension still exist and valid
arr[,,1]
# The additional partition is all NA
arr[,,5]
# Set data to 5th partition
arr[,,5] <- rep(0, 6)
# -------- Advanced usage: create fst data and import manually --------
# Clear existing files
path <- tempfile()
unlink(path, recursive = TRUE)
dir.create(path, recursive = TRUE)
# Create array of dimension 2x3x4, but 3rd partition is missing
# without using lazyarray package
# Column names must be V1 or V1R, V1I (complex)
fst::write_fst(data.frame(V1 = 1:6), path = file.path(path, 'part-1.fst'))
fst::write_fst(data.frame(V1 = 7:12), path = file.path(path, 'part-B.fst'))
fst::write_fst(data.frame(V1 = 19:24), path = file.path(path, 'part-d.fst'))
# Import via lazyarray
arr <- lazyarray(path, meta_name = 'test-int.meta',
storage_format = 'integer',
dim = c(2,3,4), prefix = 'part-',
file_names = c('1', 'B', 'C', 'd'),
quiet = TRUE)
arr[]
# Complex case
fst::write_fst(data.frame(V1R = 1:6, V1I = 1:6),
path = file.path(path, 'cplx-1.fst'))
fst::write_fst(data.frame(V1R = 7:12, V1I = 100:105),
path = file.path(path, 'cplx-2.fst'))
fst::write_fst(data.frame(V1R = 19:24, V1I = rep(0,6)),
path = file.path(path, 'cplx-4.fst'))
arr <- lazyarray(path, meta_name = 'test-cplx.meta',
storage_format = 'complex',
dim = c(2,3,4), prefix = 'cplx-',
file_names = 1:4, quiet = TRUE)
arr[]
# }
Run the code above in your browser using DataLab