1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#' Save atomic vectors to disk
#'
#' Save vectors containing atomic elements (or values that can be cast as such, e.g., dates and times) to an on-disk representation.
#'
#' @param x Any of the atomic vector types, or \link{Date} objects, or time objects, e.g., \link{POSIXct}.
#' @inheritParams saveObject
#' @param ... Further arguments that are ignored.
#'
#' @return
#' \code{x} is saved inside \code{path}.
#' \code{NULL} is invisibly returned.
#'
#' @author Aaron Lun
#'
#' @seealso
#' \code{\link{readAtomicVector}}, to read the files back into the session.
#'
#' @examples
#' tmp <- tempfile()
#' dir.create(tmp)
#' saveObject(LETTERS, file.path(tmp, "foo"))
#' saveObject(setNames(runif(26), letters), file.path(tmp, "bar"))
#' list.files(tmp, recursive=TRUE)
#'
#' @name saveAtomicVector
#' @aliases
#' stageObject,integer-method
#' stageObject,numeric-method
#' stageObject,logical-method
#' stageObject,character-method
#' stageObject,double-method
#' stageObject,POSIXct-method
#' stageObject,POSIXlt-method
#' stageObject,Date-method
NULL
.save_atomic_vector <- function(x, path, ...) {
dir.create(path)
ofile <- file.path(path, "contents.h5")
if (.is_datetime(x)) {
type <- "string"
format <- "date-time"
contents <- .sanitize_datetime(x)
} else if (is(x, "Date")) {
type <- "string"
format <- "date"
contents <- .sanitize_date(x)
} else {
stopifnot(is.atomic(x))
remapped <- .remap_atomic_type(x)
type <- remapped$type
contents <- remapped$values
format <- NULL
}
fhandle <- H5Fcreate(ofile, "H5F_ACC_TRUNC")
on.exit(H5Fclose(fhandle), add=TRUE, after=FALSE)
ghandle <- H5Gcreate(fhandle, "atomic_vector")
on.exit(H5Gclose(ghandle), add=TRUE, after=FALSE)
h5_write_attribute(ghandle, "type", type, scalar=TRUE)
if (!is.null(format)) {
h5_write_attribute(ghandle, "format", format, scalar=TRUE)
}
transformed <- transformVectorForHdf5(contents)
current <- transformed$transformed
missing.placeholder <- transformed$placeholder
dhandle <- h5_write_vector(ghandle, "values", current, emit=TRUE)
on.exit(H5Dclose(dhandle), add=TRUE, after=FALSE)
if (!is.null(missing.placeholder)) {
h5_write_attribute(dhandle, missingPlaceholderName, missing.placeholder, scalar=TRUE)
}
if (!is.null(names(x))) {
h5_write_vector(ghandle, "names", names(x))
}
saveObjectFile(path, "atomic_vector", list(atomic_vector=list(version="1.0")))
invisible(NULL)
}
#' @export
#' @rdname saveAtomicVector
setMethod("saveObject", "integer", .save_atomic_vector)
#' @export
#' @rdname saveAtomicVector
setMethod("saveObject", "character", .save_atomic_vector)
#' @export
#' @rdname saveAtomicVector
setMethod("saveObject", "logical", .save_atomic_vector)
#' @export
#' @rdname saveAtomicVector
setMethod("saveObject", "double", .save_atomic_vector)
#' @export
#' @rdname saveAtomicVector
setMethod("saveObject", "numeric", .save_atomic_vector)
#' @export
#' @rdname saveAtomicVector
setMethod("saveObject", "Date", .save_atomic_vector)
#' @export
#' @rdname saveAtomicVector
setMethod("saveObject", "POSIXlt", .save_atomic_vector)
#' @export
#' @rdname saveAtomicVector
setMethod("saveObject", "POSIXct", .save_atomic_vector)
#######################################
########### OLD STUFF HERE ############
#######################################
#' @importFrom S4Vectors DataFrame
.stage_atomic_vector <- function(x, dir, path, child=FALSE, ...) {
dir.create(file.path(dir, path), showWarnings=FALSE)
if (.is_datetime(x)) {
type <- "string"
format <- "date-time"
contents <- .sanitize_datetime(x)
} else if (is(x, "Date")) {
type <- "string"
format <- "date"
contents <- .sanitize_date(x)
} else {
stopifnot(is.atomic(x))
remapped <- .remap_atomic_type(x)
type <- remapped$type
contents <- remapped$values
format <- NULL
}
new_path <- paste0(path, "/simple.txt.gz")
mock <- DataFrame(values=contents)
if (!is.null(names(x))) {
mock <- cbind(names=names(x), mock)
}
quickWriteCsv(mock, file.path(dir, new_path), row.names=FALSE, compression="gzip")
list(
`$schema` = "atomic_vector/v1.json",
path = new_path,
is_child = child,
atomic_vector = list(
type = type,
length = length(contents),
names = !is.null(names(x)),
format = format,
compression="gzip"
)
)
}
#' @export
setMethod("stageObject", "integer", .stage_atomic_vector)
#' @export
setMethod("stageObject", "character", .stage_atomic_vector)
#' @export
setMethod("stageObject", "logical", .stage_atomic_vector)
#' @export
setMethod("stageObject", "double", .stage_atomic_vector)
#' @export
setMethod("stageObject", "numeric", .stage_atomic_vector)
#' @export
setMethod("stageObject", "Date", .stage_atomic_vector)
#' @export
setMethod("stageObject", "POSIXlt", .stage_atomic_vector)
#' @export
setMethod("stageObject", "POSIXct", .stage_atomic_vector)
|