Why we wrote wrapr to/unpack

[This article was first published on R – Win-Vector Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

To leave a comment for the author, please follow the link and comment on their blog: R – Win-Vector Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)