Skip to content
Merged

Dev #32

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
33 changes: 17 additions & 16 deletions R/ReadWriter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +962 to 963

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve converted row names in the workbook

When has_row_names = FALSE, this now-working branch calls column.2.row.names(), which removes the selected identifier column and stores it as row names. The subsequent openxlsx::write.xlsx(..., rowNames = has_row_names) still passes FALSE, so those row names are also omitted and every identifier in the selected column is silently lost from the workbook; the converted row names need to be enabled for writing on this path.

Useful? React with 👍 / 👎.

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(
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down