One reason we are developing the wrapr to/unpack methods is the following: we wanted to spruce up the R vtreat interface a bit.
We had recently back-ported a Python sklearn Pipeline step style interface from the Python vtreat to R (announcement here). But that doesn’t mean we are not continuing to make enhancements to the R style interfaces, using features that are unique to R.
With the upcoming wrapr unpacking features we can re-write use of vtreat’s mkCrossFrame*Experiment()
cross-frame interfaces from this:
# remotes::install_github("WinVector/wrapr") library(vtreat) library(wrapr) ... cross_frame_experiment <- mkCrossFrameCExperiment( d, varlist = varlist, outcomename = outcomename, outcometarget = outcometarget) # unpack the items we want from returned list treatments <- cross_frame_experiment$treatments cross_frame <- cross_frame_experiment$crossFrame
To this:
mkCrossFrameCExperiment( d, varlist = varlist, outcomename = outcomename, outcometarget = outcometarget) %.>% to[ treatments <- treatments, cross_frame <- crossFrame ]
In our first unpack note we mentioned there were R functions that returned named lists. The vtreat mkCrossFrame*Experiment()
functions are just that: functions that return named lists. Functions that return named lists/classes really call out for a named multiple assignment operator (for a positional multiple assignment operator see zeallot, and for a different named multiple assignment system see also vadr).
The assignments in a unpacking block (currently written as: to
, or unpack
) can use any of the R local assignment-like operators (<-
, ->
, =
, or :=
). And we have the convention that a name alone such as “a
” is shorthand for “a <- a
“. These unpack operators now look like a full named multi-assignment system for R.
Below is a small stand-alone example of the assignment notation.
# remotes::install_github("WinVector/wrapr") library(wrapr) to[ a_variable <- a_value, b_variable <- b_value ] <- list(a_value = 1, b_value = 2) a_variable #> [1] 1 b_variable #> [1] 2
We are working on a new vignette about named multiple assignment here.
Categories: Exciting Techniques Pragmatic Data Science Tutorials
jmount
Data Scientist and trainer at Win Vector LLC. One of the authors of Practical Data Science with R.