Skip to content
Closed
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
43 changes: 19 additions & 24 deletions R/ReadWriter.R
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ read.simple.csv.named.vector <- function(file, sep = ";", col_names = FALSE,
#' @param wRownames With row names? Default: TRUE.
#' @param NaReplace Replace NA values? Default: TRUE.
#' @param coltypes What type of variables are in columns? Auto-guessing can be very slow. Default: NULL.
#' @param asTibble Load as tibble or data frame? Default: FALSE (load as data frame).

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 Regenerate the Rd file after adding asTibble

The committed man/read.simple.ssv.Rd still shows the old signature ending at coltypes = NULL and contains no asTibble argument entry, so installed help disagrees with the exported function and users cannot discover the new option. Regenerate and commit the Rd output after changing the roxygen block and signature.

AGENTS.md reference: AGENTS.md:L21-L24

Useful? React with 👍 / 👎.

#' @examples
#' \dontrun{
#' if (interactive()) {
Expand All @@ -456,14 +457,17 @@ read.simple.csv.named.vector <- function(file, sep = ";", col_names = FALSE,
#' @importFrom gtools na.replace
read.simple.ssv <- function(
..., sep_ = " ", colnames = TRUE, wRownames = TRUE, NaReplace = TRUE,
coltypes = NULL
coltypes = NULL, asTibble = FALSE
Comment on lines 458 to +460

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Honor asTibble when replacing missing values

With the newly exposed asTibble = TRUE and the default NaReplace = TRUE, line 467 immediately wraps the result in as.data.frame(), so the function returns a data frame rather than the advertised tibble. Callers only receive a tibble if they also disable NA replacement, an undocumented coupling that makes the new option ineffective under its normal defaults.

Useful? React with 👍 / 👎.

) {
pfn <- Stringendo::kollapse(...) # merge path and filename
read_in <- suppressWarnings(readr::read_delim(pfn, delim = sep_, col_names = colnames, col_types = coltypes))
Stringendo::iprint("New variable dim: ", dim(read_in) - 0:1)

if (wRownames) read_in <- column.2.row.names(read_in, as_df = !asTibble)
if (NaReplace) read_in <- as.data.frame(gtools::na.replace(read_in, replace = 0))
if (NaReplace) {
read_in <- gtools::na.replace(read_in, replace = 0)
if (!asTibble) read_in <- as.data.frame(read_in)
}

return(read_in)
}
Expand Down Expand Up @@ -957,9 +961,10 @@ write.simple.xlsx <- function(

# assign row names if required
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)

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 the converted row names in the workbook

When has_row_names = FALSE, this conversion removes rowname_column from every sheet and stores its values as row names, but the subsequent openxlsx::write.xlsx() call still uses rowNames = has_row_names and therefore omits those row names. The function now completes instead of crashing, but silently drops the identifier column from the resulting workbook; either write the converted row names or avoid removing the column.

Useful? React with 👍 / 👎.

named_list <- lapply(named_list, assignRownames)
message("Converting column ", rowname_column, " to row names: ", head(rownames(named_list[[1]])))
has_row_names <- TRUE
}

FnP <- construct.file.path(
Expand Down Expand Up @@ -1143,34 +1148,24 @@ write.simple.md.table <- function(

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), grepl("\\.qs$", path))

# 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)
# Split into directory + base filename (without extension); write.simple.tsv()/
# write.simple.xlsx() append the correct extension themselves.
out_dir <- dirname(path)
base_filename <- sub("\\.qs$", "", basename(path))

if (out_file == "excel") {
# out_path <- ppp(base_filename, "xlsx")
Stringendo::ppp(base_filename, out_file)
ReadWriter::write.simple.xlsx(data, out_path)
}

if (out_file == "tsv") {
ReadWriter::write.simple.tsv(data, path_out, separator = "\t")
} else if (out_file == "csv") {
ReadWriter::write.simple.tsv(data, path_out, separator = ",")
} else if (out_file == "csv2") {
out_path <- paste0(base_filename, ".csv")
ReadWriter::write.simple.tsv(data, path_out, separator = ";")
if (out_file %in% c("tsv", "csv", "csv2")) {
sep <- switch(out_file, tsv = "\t", csv = ",", csv2 = ";")
ReadWriter::write.simple.tsv(data, manual_file_name = base_filename, manual_directory = out_dir, separator = sep)
} else if (out_file == "excel") {
out_path <- paste0(base_filename, ".xlsx")
ReadWriter::write.simple.xlsx(data, out_path)
payload <- if (is.data.frame(data) || is.matrix(data)) list(data) else data
ReadWriter::write.simple.xlsx(payload, manual_file_name = base_filename, manual_directory = out_dir)
}

message("File saved as: ", out_path)
}
5 changes: 4 additions & 1 deletion man/read.simple.ssv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.