Skip to content

Commit

Permalink
use brio to read lines and write lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Jul 30, 2023
1 parent e9dbc2d commit c538e55
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ Imports:
curl (>= 5.0.0),
methods,
rentrez,
xml2
xml2,
brio,
R.utils
Remotes: Rdatatable/data.table
Suggests:
BiocGenerics,
bit64,
R.utils,
stats,
stringr,
testthat (>= 3.0.0),
Expand Down
81 changes: 66 additions & 15 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,61 @@ column_to_rownames <- function(.data, var) {
}

read_lines <- function(file) {
data.table::fread(
file = file, sep = "", header = FALSE,
colClasses = "character",
showProgress = FALSE
)[[1L]]
# data.table::fread(
# file = file, sep = "", header = FALSE,
# colClasses = "character",
# showProgress = FALSE
# )[[1L]]
tmpdir <- tempdir()
file_signature <- readBin(file, raw(), 8L)
FUN <- NULL
if (endsWith(file, ".tar")) {
FUN <- utils::untar
} else if (endsWith(file, ".zip") ||
identical(
utils::head(file_signature, 4L),
charToRaw("PK\003\004")
)) {
FUN <- utils::unzip
}
if (!is.null(FUN)) {
fnames <- FUN(file, list = TRUE)
if (inherits(fnames, "data.frame")) {
fnames <- fnames[[1L]]
}
if (length(fnames) > 1L) {
cli::cli_abort("Compressed files containing more than 1 file are currently not supported.")
}
FUN(file, exdir = tmpdir)
decompFile <- file.path(tmpdir, fnames)
file <- decompFile
on.exit(unlink(decompFile), add = TRUE)
} else {
if (endsWith(file, ".gz") ||
(identical(
utils::head(file_signature, 2L),
as.raw(c(31, 139))
))) {
FUN <- gzfile
} else if (endsWith(file, ".bz2") ||
identical(
utils::head(file_signature, 3L),
as.raw(c(66, 90, 104))
)) {
FUN <- bzfile
}
if (!is.null(FUN)) {
decompFile <- tempfile(tmpdir = tmpdir)
R.utils::decompressFile(
file, decompFile,
ext = NULL, FUN = FUN,
remove = FALSE
)
file <- decompFile
on.exit(unlink(decompFile), add = TRUE)
}
}
brio::read_lines(enc2native(file))
}

# comment code to benchmark writeLines
Expand Down Expand Up @@ -94,16 +144,17 @@ read_text <- function(text, ...) {
return(data.table::data.table())
}
file <- tempfile()
data.table::fwrite(list(text),
file = file,
quote = FALSE,
na = "NA",
col.names = FALSE,
logical01 = FALSE,
showProgress = FALSE,
compress = "none",
verbose = FALSE
)
# data.table::fwrite(list(text),
# file = file,
# quote = FALSE,
# na = "NA",
# col.names = FALSE,
# logical01 = FALSE,
# showProgress = FALSE,
# compress = "none",
# verbose = FALSE
# )
brio::write_lines(text, file)
on.exit(file.remove(file))
data.table::fread(
file = file, ...,
Expand Down

0 comments on commit c538e55

Please sign in to comment.