<quosure>
expr: ^x <- 2 * exp(1)
env: global
<quosure>
expr: ^a + b + d / 2
env: global
2026-02-26
quasiquotation,
quosures,
and data masks
Idea: capture an expression with its environment (for later evaluation)
enquo and enquos (and lesser used, quo and quos):<quosure>
expr: ^x <- 2 * exp(1)
env: global
<quosure>
expr: ^a + b + d / 2
env: global
new_quosureenenv() function that captures the environment associated with an argument. (Hint: this should only require two function calls.)Sometimes we are dealing with expressions that contain both variables (symbols from a data frame) and objects from an environment
In evaluations we need to consider both the data frame and the environment
The function eval_tidy has data as a third argument; if it is specified, it will be used first when evaluating expressions:
Data masking introduces some ambiguity about the environment in which each object lives, e.g
1/mpg / mile_to_100km * gallons_to_liter
In the .data or in the parent environment .env?
gallons_to_liter = 3.79
mile_to_100km = 1.60934 /100
mtcars |>
dplyr::mutate(
lper100km = 1/mpg / mile_to_100km * gallons_to_liter
) |> dplyr::select(mpg, lper100km) |> dplyr::arrange(lper100km) mpg lper100km
Toyota Corolla 33.9 6.946911
Fiat 128 32.4 7.268527
Honda Civic 30.4 7.746719
Lotus Europa 30.4 7.746719
Fiat X1-9 27.3 8.626383
Porsche 914-2 26.0 9.057703
Merc 240D 24.4 9.651650
Datsun 710 22.8 10.328959
Merc 230 22.8 10.328959
Toyota Corona 21.5 10.953501
Hornet 4 Drive 21.4 11.004685
Volvo 142E 21.4 11.004685
Mazda RX4 21.0 11.214298
Mazda RX4 Wag 21.0 11.214298
Ferrari Dino 19.7 11.954328
Merc 280 19.2 12.265639
Pontiac Firebird 19.2 12.265639
Hornet Sportabout 18.7 12.593597
Valiant 18.1 13.011064
Merc 280C 17.8 13.230352
Merc 450SL 17.3 13.612732
Merc 450SE 16.4 14.359772
Ford Pantera L 15.8 14.905080
Dodge Challenger 15.5 15.193566
Merc 450SLC 15.2 15.493439
AMC Javelin 15.2 15.493439
Maserati Bora 15.0 15.700018
Chrysler Imperial 14.7 16.020426
Duster 360 14.3 16.468550
Camaro Z28 13.3 17.706787
Cadillac Fleetwood 10.4 22.644256
Lincoln Continental 10.4 22.644256
.data$x is always the x variable in the data frame .data
.env$x is always the x object in the environment .env
When data masking is used in programming, the expectation of R CMD check is that the ambiguity is resolved
e.g.:
ghc: no visible binding for global variable ‘cluster’
is resolved by using .data$cluster in the code (this introduces a dependence to the rlang package)
Can we implement a function my_select now that works the same way as dplyr::select?
From Advanced R
select2(mtcars, 1:2, "wt", dplyr::starts_with("d"))
Still a no.
my_select with a cheat