Closed cathblatter closed 3 years ago
I actually found a solution for my use-case, where I want the name of the object as part of the output:
library(tidyverse)
mtcars %>%
split(.$cyl) %>%
list2env(envir = .GlobalEnv)
lst(`4`, `6`, `8`) %>%
iwalk(~write_csv(.x, paste0(lubridate::today(), "cyl", .y, ".csv")))
I'm still interested in why the code from above does not work anymore 🧐 if its not with magrittr, please disregard my posts!
Thanks for your great work on the tidyverse!
This trick was relying on internal implementation details of magrittr 1.5 and thus was at high risk of breaking if the internals of magrittr changed. They were completely rewritten for the 2.0 release.
Inspired from here (https://stackoverflow.com/questions/30057278/get-lhs-object-name-when-piping-with-dplyr) I could get the name of the LHS-object with this function:
As you can see, $name now returns NULL, when I would expect
head(mtcars)
.It was useful to have the name of the object in the filename when I used it like this:
object1 %>% write_csv(., file = paste0(lubridate::today(), get_orig_name(.)$name, ".csv"))
I asked on community.rstudio.com (https://community.rstudio.com/t/name-of-lhs-object-when-using-pipe-returns-null/91479) and on SO others experience the same behaviour (https://stackoverflow.com/questions/42560389/get-name-of-dataframe-passed-through-pipe-in-r/42561430#42561430) - but I couldn't get an answer.
I am not sure if this qualifies as an issue/bug, but I'm interested in knowing where this comes from! Does this have to do with the latest version of magrittr or is my issue related to smth different?
thanks for any inputs!
my sessioninfo: