Skip to content

Commit

Permalink
refactor outfile creation in export_list
Browse files Browse the repository at this point in the history
  • Loading branch information
schochastics committed Sep 11, 2023
1 parent a51350d commit 4a50411
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
28 changes: 2 additions & 26 deletions R/export_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,8 @@ export_list <- function(x, file, archive = "", ...) {
stop("'x' must be a list. Perhaps you want export()?")
}

if (is.null(file)) {
stop("'file' must be a character vector")
} else if (length(file) == 1L) {
if (!grepl("%s", file, fixed = TRUE)) {
stop("'file' must have a %s placeholder")
}
if (is.null(names(x))) {
outfiles <- sprintf(file, seq_along(x))
} else {
if (any(nchar(names(x))) == 0) {
stop("All elements of 'x' must be named or all must be unnamed")
}
if (anyDuplicated(names(x))) {
stop("Names of elements in 'x' are not unique")
}
outfiles <- sprintf(file, names(x))
}
} else {
if (length(x) != length(file)) {
stop("'file' must be same length as 'x', or a single pattern with a %s placeholder")
}
if (anyDuplicated(file)) {
stop("File names are not unique")
}
outfiles <- file
}
outfiles <- .create_outfiles(file, names(x))

if (is.na(archive_format$compress) && archive_format$file != "") {
outfiles <- file.path(archive_format$file, outfiles)
}
Expand Down
29 changes: 27 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ get_ext <- function(file) {


.query_format <- function(input, file) {
unique_rio_formats <- unique(rio_formats[,colnames(rio_formats) != "note"])
unique_rio_formats <- unique(rio_formats[, colnames(rio_formats) != "note"])
if (file == "clipboard") {
output <- as.list(unique_rio_formats[unique_rio_formats$format == "clipboard",])
output <- as.list(unique_rio_formats[unique_rio_formats$format == "clipboard", ])
output$file <- file
return(output)
}
Expand Down Expand Up @@ -114,3 +114,28 @@ escape_xml <- function(x, replacement = c("&amp;", "&quot;", "&lt;", "&gt;", "&a
}
invisible(NULL)
}

.create_outfiles <- function(file, names_x) {
if (length(file) == 1L) {
if (!grepl("%s", file, fixed = TRUE)) {
stop("'file' must have a %s placeholder")
}
if (is.null(names_x)) {
return(sprintf(file, seq_along(x)))
}
if (any(nchar(names_x)) == 0) {
stop("All elements of 'x' must be named or all must be unnamed")
}
if (anyDuplicated(names_x)) {
stop("Names of elements in 'x' are not unique")
}
return(sprintf(file, names_x))
}
if (length(x) != length(file)) {
stop("'file' must be same length as 'x', or a single pattern with a %s placeholder")
}
if (anyDuplicated(file)) {
stop("File names are not unique")
}
return(file)
}

0 comments on commit 4a50411

Please sign in to comment.