# \donttest{
tmp <- tempfile(fileext = ".h5")
X <- hdf5_create_matrix(tmp, "data/M", data = matrix(rnorm(1000), 100, 10))
pca <- prcomp(X, center = TRUE, scale. = FALSE)
cat("Variance explained (PC1-3):", pca$cumvar[1:3], "\n")
dim(pca$rotation) # 10 x nPC
dim(pca$x) # 100 x nPC
# rank. takes precedence over ncomponents when both are supplied.
# NOTE: this reuses the same output location as the call above, so it
# must come after pca's results have already been read/used -- once
# overwrite = TRUE recreates the output datasets, any earlier
# HDF5Matrix handle pointing at that location (here, pca$rotation and
# pca$x) becomes invalid by design (see HDF5Matrix lifecycle, Section
# 5.3.3): the package proactively clears stale external pointers when
# overwriting to convert what would otherwise be a memory-unsafe crash
# into a clean R-level error.
pca5 <- prcomp(X, rank. = 5, ncomponents = 10, overwrite = TRUE)
dim(pca5$rotation) # 10 x 5 -- rank. (5) was used, not ncomponents (10)
hdf5_close_all()
unlink(tmp)
# }
Run the code above in your browser using DataLab