tidyverse / purrr

A functional programming toolkit for R
https://purrr.tidyverse.org/
Other
1.27k stars 274 forks source link

list_rbind overwrites data.frame name without warning if argument names_to is provided. #1120

Closed cdolladille closed 3 months ago

cdolladille commented 8 months ago

Hello,

I started using list_rbind() recently and found the following behavior : if arg names_to is provided with a value that is also a column name of the map output, then the column is overwritten.

c("t1", "t2") |> 
  rlang::set_names() |> 
  map(function(t_)
    data.frame(
      a = "expected value")
    ) |> 
  list_rbind()

Returns

a expected value expected value

c("t1", "t2") |> 
  rlang::set_names() |> 
  map(function(t_)
    data.frame(
      a = "expected value")
  ) |> 
  list_rbind(names_to = "a")

Returns

a t1 t2

That may be an intended behavior but I can't find it documented in the ?list_rbind() help. A message, a warning or a documentation could help?

Sorry if I missed the place where it's written, and thank you so much for developping all these consistent packages!

hadley commented 3 months ago

I think this is the behaviour we use consistently across the tidyverse — if you supply the name of a variable where output should go, it will overwrite any existing variable with that name. So that means we either have to document it everywhere (since otherwise folks will be confused when it's not documented), or nowhere. And since other functions that use the principle (like dplyr::mutate() and tidyr::separate_wider_*) don't document it, it looks like nowhere is the better better.