Skip to content

Commit b2a9860

Browse files
committed
Improve epix_merge() error message, tweak format_chr_with_quotes()
1 parent 01a9d9f commit b2a9860

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

R/key_colnames.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ key_colnames.epi_df <- function(x, ...,
9191
if (!identical(other_keys, expected_other_keys)) {
9292
cli_abort(c(
9393
"The provided `other_keys` argument didn't match the `other_keys` of `x`",
94-
"*" = "`other_keys` was {format_chr_with_quotes(other_keys)}",
95-
"*" = "`expected_other_keys` was {format_chr_with_quotes(expected_other_keys)}",
94+
"*" = "`other_keys` was {format_chr_deparse(other_keys)}",
95+
"*" = "`expected_other_keys` was {format_chr_deparse(expected_other_keys)}",
9696
"i" = "If you know that `x` will always be an `epi_df` and
9797
resolve this discrepancy by adjusting the metadata of `x`, you
9898
shouldn't have to pass `other_keys =` here anymore,

R/methods-epi_archive.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ epix_merge <- function(x, y,
450450
y_nonby_colnames <- setdiff(names(y_dt), by)
451451
if (length(intersect(x_nonby_colnames, y_nonby_colnames)) != 0L) {
452452
cli_abort("
453-
`x` and `y` DTs have overlapping non-by column names;
453+
`x` and `y` DTs both have measurement columns named
454+
{format_chr_with_quotes(intersect(x_nonby_colnames, y_nonby_colnames))};
454455
this is currently not supported; please manually fix up first:
455456
any overlapping columns that can are key-like should be
456457
incorporated into the key, and other columns should be renamed.

R/utils.R

+17-14
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,29 @@ format_chr_deparse <- function(x) {
9898
paste(collapse = "", deparse(x))
9999
}
100100

101-
#' Format a character vector as a string via deparsing/quoting each
101+
#' Format each entry in a character vector via quoting; special replacement for length 0
102+
#'
103+
#' Performs no escaping within the strings; if you want something that reader
104+
#' could copy-paste to debug, look into `format_deparse` (note that this
105+
#' collapses into a single string).
106+
#'
107+
#' @param x chr; e.g., `colnames` of some data frame
108+
#' @param empty chr, likely string; what should be output if `x` is of length 0?
109+
#' @return chr; same `length` as `x` if `x` had nonzero length; value of `empty` otherwise
110+
#'
111+
#' @examples
112+
#' cli::cli_inform('{format_chr_with_quotes("x")}')
113+
#' cli::cli_inform('{format_chr_with_quotes(c("x","y"))}')
114+
#' nms <- c("x","\"Total Cases\"")
115+
#' cli::cli_inform('{format_chr_with_quotes(nms)}')
116+
#' cli::cli_inform('{format_chr_with_quotes(character())}')
102117
#'
103-
#' @param x `chr`; e.g., `colnames` of some data frame
104-
#' @param empty string; what should be output if `x` is of length 0?
105-
#' @return string
106118
#' @keywords internal
107119
format_chr_with_quotes <- function(x, empty = "*none*") {
108120
if (length(x) == 0L) {
109121
empty
110122
} else {
111-
# Deparse to get quoted + escape-sequenced versions of varnames; collapse to
112-
# single line (assuming no newlines in `x`). Though if we hand this to cli
113-
# it may insert them (even in middle of quotes) while wrapping lines.
114-
deparsed_collapsed <- paste(collapse = "", deparse(x))
115-
if (length(x) == 1L) {
116-
deparsed_collapsed
117-
} else {
118-
# remove surrounding `c()`:
119-
substr(deparsed_collapsed, 3L, nchar(deparsed_collapsed) - 1L)
120-
}
123+
paste0('"', x, '"')
121124
}
122125
}
123126

man/format_chr_with_quotes.Rd

+15-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-epix_merge.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ test_that("epix_merge forbids and warns on metadata and naming issues", {
175175
as_epi_archive(tibble::tibble(geo_value = "ak", time_value = test_date, version = test_date + 1L, value = 1L)),
176176
as_epi_archive(tibble::tibble(geo_value = "ak", time_value = test_date, version = test_date + 1L, value = 2L))
177177
),
178-
regexp = "overlapping.*names"
178+
regexp = 'both have measurement columns named "value"'
179179
)
180180
})
181181

0 commit comments

Comments
 (0)