Another riff on the placeholder idea with |>
:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
. <- function(.dat, template){
template_code <- deparse(substitute(template))
arg <- deparse(substitute(.dat))
interpolated_code <- gsub("(?<=[(, ])?[.](?=[), \\[])", arg, template_code, perl = TRUE)
eval(parse( text = interpolated_code))
}
"a" |>
.(c(., "b")) |>
.(setNames(., .))
#> a b
#> "a" "b"
mtcars |>
transform(kmL = mpg / 2.35) |>
.(lm(kmL ~ hp, data = .))
#>
#> Call:
#> lm(formula = kmL ~ hp, data = transform(mtcars, kmL = mpg/2.35))
#>
#> Coefficients:
#> (Intercept) hp
#> 12.80803 -0.02903
"col_name" |>
.(mutate(mtcars, . = "cool")) |>
.(bind_cols(., .)) |>
.(.[1, ])
#> New names:
#> * mpg -> mpg...1
#> * cyl -> cyl...2
#> * disp -> disp...3
#> * hp -> hp...4
#> * drat -> drat...5
#> * ...
#> mpg...1 cyl...2 disp...3 hp...4 drat...5 wt...6 qsec...7 vs...8
#> Mazda RX4 21 6 160 110 3.9 2.62 16.46 0
#> am...9 gear...10 carb...11 col_name...12 mpg...13 cyl...14 disp...15
#> Mazda RX4 1 4 4 cool 21 6 160
#> hp...16 drat...17 wt...18 qsec...19 vs...20 am...21 gear...22
#> Mazda RX4 110 3.9 2.62 16.46 0 1 4
#> carb...23 col_name...24
#> Mazda RX4 4 cool
Created on 2021-06-24 by the reprex package (v2.0.0)
I call .
the ‘neutering’ function.