-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconvert.R
More file actions
27 lines (26 loc) · 883 Bytes
/
convert.R
File metadata and controls
27 lines (26 loc) · 883 Bytes
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
#' Convert a data.frame to Arrow IPC stream bytes
#' @param df A data.frame.
#' @return Raw vector of Arrow IPC stream bytes.
#' @noRd
df_to_ipc <- function(df) {
# Convert factors to character — nanoarrow doesn't support dictionary IPC encoding
factor_cols <- vapply(df, is.factor, logical(1))
if (any(factor_cols)) {
df[factor_cols] <- lapply(df[factor_cols], as.character)
}
stream <- nanoarrow::as_nanoarrow_array_stream(df)
con <- rawConnection(raw(0), "wb")
on.exit(close(con))
nanoarrow::write_nanoarrow(stream, con)
rawConnectionValue(con)
}
#' Convert Arrow IPC stream bytes to a data.frame
#' @param ipc_bytes Raw vector of Arrow IPC stream bytes.
#' @return A data.frame.
#' @noRd
ipc_to_df <- function(ipc_bytes) {
con <- rawConnection(ipc_bytes, "rb")
on.exit(close(con))
stream <- nanoarrow::read_nanoarrow(con)
as.data.frame(stream)
}