In defense of wrapr::let()
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Saw this the other day:
In defense of wrapr::let()
(originally part of replyr
, and still re-exported by that package) I would say:
let()
was deliberately designed for a single real-world use case: working with data when you don’t know the column names when you are writing the code (i.e., the column names will come later in a variable). We can re-phrase that as: there is deliberately less to learn aslet()
is adapted to a need (instead of one having to adapt tolet()
).- The
R
community already has months of experience confirminglet()
working reliably in production while interacting with a number of different packages. let()
will continue to be a very specific, consistent, reliable, and relevant tool even afterdpyr 0.6.*
is released, and the community gains experience withrlang
/tidyeval
in production.
If rlang
/tidyeval
is your thing, by all means please use and teach it. But please continue to consider also using wrapr::let()
. If you are trying to get something done quickly, or trying to share work with others: a “deeper theory” may not be the best choice.
An example follows.
In “base R” one can write:d <- data.frame(x = 1:3)
If we know the column name we wish to add to this data frame we write:
d$newColumn <- 1
The above is “non-parameterized” evaluation in that the variable name is taken from the source code, and not from a variable carrying the information. This is great for ad-hoc analysis, but it would be hard to write functions, scripts and packages if this was the only way we had to manipulate columns.
This isn’t a problem as R
supplies an additional parameterized notation as we show here. When we don’t know the name of the column (but expect it to be in a variable at run time) we write:
# code written very far away from us variableHoldingColumnName <- 'newColumn' # our code d[[variableHoldingColumnName]] <- 1
The purpose of wrapr::let()
is to allow one to use the non-parameterized form as if it were parameterized. Obviously this is only useful if there is not a convenient parameterized alternative (which is the case for some packages). But for teaching purposes: how would wrapr::let()
let us use the “$
” as if it were parameterized (which we would have to do if [[]]
and []
did not exist)?
With wrapr::let()
we can re-write the “dollar-sign form” as:
wrapr::let( c(NEWCOL = variableHoldingColumnName), { d$NEWCOL <- 1 } )
The name “NEWCOL
” is a stand-in name that we write all our code in terms of. The expression “c(NEWCOL = variableHoldingColumnName)
” is saying: “NEWCOL
is to be interpreted as being whatever name is stored in the variable variableHoldingColumnName
.” Notice we can’t tell from the code what value is in variableHoldingColumnName
, as that won’t be known until execution time. The alias list or vector can be arbitrarily long and built wherever you like (it is just data). The expression block can be arbitrarily large and complex (so you need only pay the mapping notation cost once per function, not once per line of code).
And that is wrapr::let()
.
If you don’t need parametric names you don’t need wrapr::let()
. If you do need parametric names wrapr::let()
is one of the most reliable and easiest to learn ways to get them.
A nice article from a user recording their experience trying different ways to parameterize their code (including wrapr::let()
) can be found here.
If you wish to learn more we have a lot of resources available:
- A vignette:
vignette('let', package='wrapr')
. - The method help:
help('let', package='wrapr')
- Examples: Using replyr::let to Parameterize dplyr Expressions.
- A video lecture: My recent BARUG talk: Parametric Programming in R with replyr.
- Some notes: Parametric Programming in R.
- The package introduction: The
wrapr
introduction. - Comparison to the soon to deprecated SE underbar/underscore methods: Comparative examples using replyr::let.
And many more examples on our 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.