Skip to content

Name columns from named non-symbol arguments in xnew_data() - #110

Merged
joethorley merged 2 commits into
fix-99-named-symbol-columnfrom
fix-109-named-non-symbol
Sep 8, 2026
Merged

joethorley merged 2 commits into
fix-99-named-symbol-columnfrom
fix-109-named-non-symbol

Conversation

@joethorley

Copy link
Copy Markdown
Member

Closes #109.

Note

Stacked on #102, which this builds on directly.
Base is fix-99-named-symbol-column; retarget to main once #102 merges.

#102 applied the argument name to the value only for bare symbols.
Every other named argument reached tidyr::expand() with its name intact, which gave two
bugs:

d <- tibble::tibble(a = 1:5 + 0.5, b = factor(letters[1:5]))

xnew_data(d, z = new_seq(b, .length_out = 2))$z   # bare vector
#> before: a b c d e      after: a b

names(xnew_data(d, z = xnew_seq(b, .length_out = 2)))   # one column tibble
#> before: "a" "b" "z"  where z is z$b       after: "a" "b" "z"  where z is a factor

The first is the idiom in xnew_seq()'s own examples, so it was reachable from the
documentation.

Approach

expand() sorts and deduplicates an argument's values whether it is a bare vector or a
data frame, so that is not the distinction.
The one behaviour that differs is factors: a bare factor vector is expanded to all of its
levels, whereas a factor held in a data frame keeps only the values present.
That level expansion is what discarded .length_out and .obs_only.

xnew_column() therefore gives every named argument the treatment #102 gave named symbols:

  • a bare vector is wrapped in a one column tibble named after the argument;
  • a one column data frame has that column renamed;
  • a multi column data frame is packed into a named data frame column, exactly as today.

The dispatch is on the value rather than the expression because the type is not knowable
statically, so the helper is inlined into the quosure -- it is internal, and the quosure is
evaluated in the caller's environment where it would not otherwise be in scope.

The outer name is now dropped for every named argument rather than only for symbols, which
also removes the map_lgl() scan of the quosures.

Checks

  • R CMD check: 0 errors, 0 warnings, 0 notes.
  • Full test suite passes with no snapshot changes; the existing named-argument snapshots
    (b = 8:10, z = "zed", a = new_value(a)) are unaffected because those values are
    already sorted and unique.
  • New tests cover the bare vector, one column data frame and multi column data frame cases.

`xnew_data()` only applied the argument name to the value for bare symbols,
so a named non-symbol argument reached `tidyr::expand()` with its name intact.
A bare factor vector was then expanded to all of its levels, discarding
`.length_out` and `.obs_only`, and a one column data frame became a packed
data frame column.

`xnew_column()` now applies the name to the value for every named argument,
dispatching on the value since its type is not knowable from the expression.
A multi column data frame stays packed as before.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@joethorley
joethorley merged commit 634900d into fix-99-named-symbol-column Sep 8, 2026
@joethorley
joethorley deleted the fix-109-named-non-symbol branch September 8, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant