tidyverse / magrittr

Improve the readability of R code with the pipe
https://magrittr.tidyverse.org
Other
957 stars 157 forks source link

Name of LHS-object returns NULL #241

Closed cathblatter closed 3 years ago

cathblatter commented 3 years ago

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:

library(tidyverse)

get_orig_name <- function(df){
  i <- 1
  while(!("chain_parts" %in% ls(envir=parent.frame(i))) && i < sys.nframe()) {
    i <- i+1
  }
  list(name = deparse(parent.frame(i)$lhs), 
       output = df)
}

head(mtcars) %>% get_orig_name(.)
#> $name
#> [1] "NULL"
#> 
#> $output
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

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:

R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] shiny_1.5.0     forcats_0.5.0   stringr_1.4.0   dplyr_1.0.2     purrr_0.3.4     readr_1.4.0     tidyr_1.1.2     tibble_3.0.4   
 [9] ggplot2_3.3.2   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] httr_1.4.2        pkgload_1.1.0     jsonlite_1.7.2    modelr_0.1.8      assertthat_0.2.1  blob_1.2.1        cellranger_1.1.0 
 [8] remotes_2.2.0     sessioninfo_1.1.1 pillar_1.4.7      backports_1.2.1   glue_1.4.2        digest_0.6.27     promises_1.1.1   
[15] rvest_0.3.6       colorspace_2.0-0  htmltools_0.5.0   httpuv_1.5.4      clipr_0.7.1       pkgconfig_2.0.3   devtools_2.3.1   
[22] broom_0.7.3       haven_2.3.1       xtable_1.8-4      scales_1.1.1      processx_3.4.5    whisker_0.4       later_1.1.0.1    
[29] generics_0.1.0    usethis_1.6.1     ellipsis_0.3.1    withr_2.3.0       cli_2.2.0         magrittr_2.0.1    crayon_1.3.4     
[36] readxl_1.3.1      mime_0.9          memoise_1.1.0     evaluate_0.14     ps_1.5.0          fs_1.5.0          fansi_0.4.1      
[43] xml2_1.3.2        pkgbuild_1.2.0    tools_4.0.3       prettyunits_1.1.1 hms_0.5.3         lifecycle_0.2.0   munsell_0.5.0    
[50] reprex_0.3.0      callr_3.5.1       compiler_4.0.3    rlang_0.4.9       grid_4.0.3        rstudioapi_0.13   miniUI_0.1.1.1   
[57] rmarkdown_2.6     testthat_3.0.1    gtable_0.3.0      DBI_1.1.0         R6_2.5.0          lubridate_1.7.9.2 knitr_1.30       
[64] fastmap_1.0.1     rprojroot_2.0.2   desc_1.2.0        stringi_1.5.3     Rcpp_1.0.5        vctrs_0.3.6       dbplyr_1.4.4     
[71] tidyselect_1.1.0  xfun_0.19    
cathblatter commented 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!

lionel- commented 3 years ago

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.