diff --git a/DESCRIPTION b/DESCRIPTION index 1b1aebc..f4f3d37 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,11 +9,12 @@ License: GPL-3 + file LICENSE BugReports: https://github.com/vertesy/ReadWriter/issues Depends: Stringendo (>= 0.5.0) -Imports: +Imports: gtools, openxlsx, qs, - readr + readr, + stringi Encoding: UTF-8 Packaged: 2026-08-25 16:41:37.734198 Roxygen: list(markdown = TRUE) diff --git a/NAMESPACE b/NAMESPACE index 035a34a..5d57af3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -33,3 +33,4 @@ importFrom(readr,read_csv) importFrom(readr,read_csv2) importFrom(readr,read_delim) importFrom(readr,read_tsv) +importFrom(stringi,stri_detect_regex) diff --git a/R/ReadWriter.R b/R/ReadWriter.R index 1e67a16..c197c54 100644 --- a/R/ReadWriter.R +++ b/R/ReadWriter.R @@ -957,10 +957,12 @@ write.simple.xlsx <- function( ) # assign row names if required + write_row_names <- has_row_names if (isFALSE(has_row_names)) { - assignRownames <- function(x) column.2.row.names(df, rowname_column = rowname_column, make_names = TRUE) + assignRownames <- function(x) column.2.row.names(x, rowname_column = rowname_column, make_names = TRUE) named_list <- lapply(named_list, assignRownames) message("Converting column ", rowname_column, " to row names: ", head(rownames(named_list[[1]]))) + write_row_names <- TRUE # the converted identifiers must still be written as the sheet's row names } FnP <- construct.file.path( @@ -970,7 +972,7 @@ write.simple.xlsx <- function( ) openxlsx::write.xlsx( - x = named_list, file = FnP, rowNames = has_row_names, + x = named_list, file = FnP, rowNames = write_row_names, firstRow = FreezeFirstRow, firstCol = FreezeFirstCol, headerStyle = hs, tabColour = TabColor, colWidths = "auto", creator = Creator @@ -1139,38 +1141,37 @@ write.simple.md.table <- function( #' @return The function does not return a value but writes the file to disk in the specified format. #' #' @importFrom qs qread +#' @importFrom stringi stri_detect_regex #' #' @export qs.2.table qs.2.table <- function(path, out_file = c("tsv", "csv", "csv2", "excel")[1]) { # Ensure that the file exists and is a .qs file - stopifnot(file.exists(path), stringi::stri_detect(str = path, regex = "\\.qs$")) + stopifnot(file.exists(path), stringi::stri_detect_regex(str = path, pattern = "\\.qs$")) # Ensure out_file is one of the allowed choices out_file <- match.arg(out_file, c("tsv", "csv", "csv2", "excel")) # Read in the .qs file - data <- qs:qread(path) + data <- qs::qread(path) - # Determine the output file extension and write the file based on the output format - path_out <- Stringendo::ppp(base_filename, out_file) - - if (out_file == "excel") { - # out_path <- ppp(base_filename, "xlsx") - Stringendo::ppp(base_filename, out_file) - ReadWriter::write.simple.xlsx(data, out_path) - } + # Base name (without extension) shared by all output formats + base_filename <- sub("\\.qs$", "", path) if (out_file == "tsv") { - ReadWriter::write.simple.tsv(data, path_out, separator = "\t") + ReadWriter::write.simple.tsv(data, manual_file_name = base_filename, separator = "\t") + out_path <- paste0(base_filename, ".tsv") } else if (out_file == "csv") { - ReadWriter::write.simple.tsv(data, path_out, separator = ",") + ReadWriter::write.simple.tsv(data, manual_file_name = base_filename, separator = ",") + out_path <- paste0(base_filename, ".csv") } else if (out_file == "csv2") { + ReadWriter::write.simple.tsv(data, manual_file_name = base_filename, separator = ";") out_path <- paste0(base_filename, ".csv") - ReadWriter::write.simple.tsv(data, path_out, separator = ";") } else if (out_file == "excel") { + # write.simple.xlsx() expects a list of sheets; wrap a bare table into one. + payload <- if (is.data.frame(data) || is.matrix(data)) list(data) else data + ReadWriter::write.simple.xlsx(payload, manual_file_name = base_filename) out_path <- paste0(base_filename, ".xlsx") - ReadWriter::write.simple.xlsx(data, out_path) } message("File saved as: ", out_path)