The R
package wrapr 1.5.0 is now available on CRAN.
wrapr includes a lot of tools for writing better R
code:
let()
(let block)%.>%
(dot arrow pipe)build_frame()
/draw_frame()
(data.frame
builders and formatters )qc()
(quoting concatenate):=
(named map builder)%?%
(coalesce) NEW!%.|%
(reduce/expand args) NEW!uniques()
(safeunique()
replacement) NEW!partition_tables()
/execute_parallel()
NEW!DebugFnW()
(function debug wrappers)λ()
(anonymous function builder)
I’ll be writing articles on a number of the new capabilities. For now I just leave you with the nifty operator coalesce notation.
Coalesce takes values from its arguments in left to right order taking the first non-NA
value (if any available):
NA %?% 5 # [1] 5 1 %?% 5 # [1] 1 5 %?% NA # [1] 5
For vectors each position is calculated independently, and scalars are re-cycled to vector sizes. This allows fairly complicated coalesce strategies (such as take from first two vectors if possible, else write in zero) to be expressed very succinctly:
vec1 <- c(1, 2, NA, NA) vec2 <- c(10, NA, 20, NA) vec1 %?% vec2 %?% 0 # [1] 1 2 20 0
Categories: Administrativia Coding
jmount
Data Scientist and trainer at Win Vector LLC. One of the authors of Practical Data Science with R.
Kind of proud of the following small improvement in dot_arrow: