Skip to content

Instantly share code, notes, and snippets.

@codez0mb1e
Created September 9, 2020 13:08
Show Gist options
  • Save codez0mb1e/e1f3873188393d2dc94e9a2effdf94a2 to your computer and use it in GitHub Desktop.
Save codez0mb1e/e1f3873188393d2dc94e9a2effdf94a2 to your computer and use it in GitHub Desktop.
IO functions wrapper
#'
#' IO functions wrapper
#'
#' Write dataset to CSV
#'
#' @param dt
#' @param .file_name
#' @param .col_names
#' @param .eol
#' @param .append
#' @param .row_names
#' @param .quote
#' @param .sep
#' @param .fill_na
#' @param .encoding
#' @param .verbose
#'
io.write_to_csv <- function(dt,
.file_name,
.append = T,
.col_names = T,
.row_names = F,
.quote = T,
.sep = ",",
.fill_na = "",
.eol = "\n",
.encoding = "UTF-8",
.verbose = 0L) {
stopifnot(
is.data.frame(dt),
is.character(.file_name),
is.logical(.col_names),
is.logical(.row_names),
is.logical(.quote),
is.character(.sep),
is.character(.fill_na),
is.character(.eol),
is.character(.encoding),
is.numeric(.verbose)
)
if (.verbose != 0)
print(sprintf("Starting to write %s [%s]...", .file_name, format(object.size(dt), units = "KB")))
write.table(dt, file = .file_name, append = .append,
quote = .quote, row.names = .row_names, col.names = .col_names,
na = .fill_na, sep = .sep, eol = .eol,
fileEncoding = .encoding)
}
testthat::test_that("io.write_to_csv test", {
data <- datasets::iris
io.write_to_csv(data, "write_iris.csv", .verbose = 1)
})
#' Write to GZip file
#'
#' @param dt
#' @param .quote
#' @param .row_names
#' @param .file_name
#' @param .append
#' @param .col_names
#' @param .sep
#' @param .fill_na
#' @param .eol
#' @param .encoding
#' @param .verbose
#'
io.write_to_gzip <- function(dt,
.file_name,
.append = T,
.col_names = T,
.row_names = F,
.quote = T,
.sep = ",",
.fill_na = "",
.eol = "\n",
.encoding = "UTF-8",
.verbose = 0L) {
require(dplyr)
stopifnot(
is.data.frame(dt),
is.character(.file_name),
is.logical(.col_names),
is.logical(.row_names),
is.logical(.quote),
is.character(.sep),
is.character(.fill_na),
is.character(.eol),
is.character(.encoding),
is.numeric(.verbose)
)
tryCatch({
gzFile <- gzfile(sprintf("%s.csv.gz", .file_name), "w")
if (.verbose != 0)
print(sprintf("Starting to write %s [%s]...", gzFile, format(object.size(dt), units = "KB")))
write.csv(dt, file = gzFile, append = .append,
quote = .quote, row.names = .row_names, col.names = .col_names,
na = .fill_na, sep = .sep, eol = .eol,
fileEncoding = .encoding)
}, finally = {
close(gzFile)
})
}
testthat::test_that("io.write_to_gzip test", {
data <- datasets::iris
io.write_to_gzip(data, "write_iris_zip", .verbose = 1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment