nathaneastwood / poorman

A poor man's dependency free grammar of data manipulation
https://nathaneastwood.github.io/poorman/
Other
340 stars 15 forks source link

fix: relocate() works when .before and .after called in a function #114

Closed etiennebacher closed 2 years ago

etiennebacher commented 2 years ago

Close #110. I didn't use the code you put in #110 because it didn't seem to work. Maybe my solution can be simplified.

myFun <- function(location_before = NULL, location_after = NULL) {
  poorman::relocate(head(mtcars), gear, .before = location_before, .after = location_after)
}
myFun()
#>                   gear  mpg cyl disp  hp drat    wt  qsec vs am carb
#> Mazda RX4            4 21.0   6  160 110 3.90 2.620 16.46  0  1    4
#> Mazda RX4 Wag        4 21.0   6  160 110 3.90 2.875 17.02  0  1    4
#> Datsun 710           4 22.8   4  108  93 3.85 2.320 18.61  1  1    1
#> Hornet 4 Drive       3 21.4   6  258 110 3.08 3.215 19.44  1  0    1
#> Hornet Sportabout    3 18.7   8  360 175 3.15 3.440 17.02  0  0    2
#> Valiant              3 18.1   6  225 105 2.76 3.460 20.22  1  0    1
myFun(location_before = 2)
#>                    mpg gear cyl disp  hp drat    wt  qsec vs am carb
#> Mazda RX4         21.0    4   6  160 110 3.90 2.620 16.46  0  1    4
#> Mazda RX4 Wag     21.0    4   6  160 110 3.90 2.875 17.02  0  1    4
#> Datsun 710        22.8    4   4  108  93 3.85 2.320 18.61  1  1    1
#> Hornet 4 Drive    21.4    3   6  258 110 3.08 3.215 19.44  1  0    1
#> Hornet Sportabout 18.7    3   8  360 175 3.15 3.440 17.02  0  0    2
#> Valiant           18.1    3   6  225 105 2.76 3.460 20.22  1  0    1
myFun(location_after = 2)
#>                   gear  mpg cyl disp  hp drat    wt  qsec vs am carb
#> Mazda RX4            4 21.0   6  160 110 3.90 2.620 16.46  0  1    4
#> Mazda RX4 Wag        4 21.0   6  160 110 3.90 2.875 17.02  0  1    4
#> Datsun 710           4 22.8   4  108  93 3.85 2.320 18.61  1  1    1
#> Hornet 4 Drive       3 21.4   6  258 110 3.08 3.215 19.44  1  0    1
#> Hornet Sportabout    3 18.7   8  360 175 3.15 3.440 17.02  0  0    2
#> Valiant              3 18.1   6  225 105 2.76 3.460 20.22  1  0    1

Created on 2022-08-27 by the reprex package (v2.0.1)

Only works if the argument passed are numeric.

codecov-commenter commented 2 years ago

Codecov Report

Merging #114 (54fdb68) into master (bbb9aed) will decrease coverage by 0.08%. The diff coverage is 85.71%.

@@            Coverage Diff             @@
##           master     #114      +/-   ##
==========================================
- Coverage   93.53%   93.45%   -0.09%     
==========================================
  Files          58       58              
  Lines        1455     1467      +12     
==========================================
+ Hits         1361     1371      +10     
- Misses         94       96       +2     
Impacted Files Coverage Δ
R/relocate.R 94.44% <85.71%> (-5.56%) :arrow_down:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

etiennebacher commented 2 years ago

My fix fails when using column names e.g df %>% relocate(a, .after = c). There must be a cleaner way to fix this.