0

Is there a way to know if a table was renamed in the process of unnesting? I want to know if there is something where I can intercept any messages that come through with New names: and give more context about solutions

# min reprex
library(tidyverse)

f <- function() { 
  tibble(
    x = 1:2, 
    y = 2:1, 
    z = tibble(x = 1)
  ) |> 
    unnest_wider(z, names_repair = "unique")
}

f()
New names:
• `x` -> `x...1`
• `x` -> `x...3`

 x...1     y x...3
 ----- ----- -----
     1     2     1
     2     1     1

More context:

  • The message stems from

    vctrs::vec_as_names(c("x", "x"), repair = "universal")
    
  • I see information about withCallingHandlers() but not sure if that is the right route. I thought there was a way for errors/messages to have classes that you can intercept but I can't remember what I read.

  • Something in testthat::expect_message() may help. I thought there would be a has_message() function out there.

  • There is a lot of tidy evaluation and comparing names before and after might be tricky. I could look for the names with the regex "\\.+\\d+$" but not sure that is robust enough since data could have fields with that syntax already.

M--
  • 25,431
  • 8
  • 61
  • 93
yake84
  • 3,004
  • 2
  • 19
  • 35
  • Have you taken a look at this [question](https://stackoverflow.com/questions/4948361/how-do-i-save-warnings-and-errors-as-output-from-a-function)? – Ritchie Sacramento May 20 '22 at 06:07

1 Answers1

0

Taking inspiration from hadley's answer, on the question that @ritchie-sacramento linked, you should check out the evaluate package.

> eval_res <- evaluate::evaluate("f()")
> eval_res[[2]]$message
[1] "New names:\n* x -> x...1\n* x -> x...3\n"

This will require more testing to see what happens to the data structure when there are errors, warnings, or even multiple messages. But this seems like the right track.

Paul Wildenhain
  • 1,321
  • 10
  • 12