knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
dub is a small, single-purpose R package for unpacking assignment: it provides an operator %<<-%
that enables you to assign (nested) components of a list (or vector) to names via pattern matching. The pattern matching syntax mirrors the semantics of list()
. Think of the “dub(ble) arrow” <<-
as a pictograph representing multiple <-
's.
library(dub) (one : two : three) %<<-% 1:3 one two three (x) %<<-% list(list("x")) ((y)) %<<-% list(list("y")) x y (u : (v : w)) %<<-% list(1, list(2, 3:4)) u v w # Use . to drop specific components, ... to drop greedily (the _ in Haskell) (. : width : ... : species) %<<-% iris head(width) head(species)
More details and examples are in the package documentation (?`%<<-%`
).
Install from CRAN:
``````r install.packages("dub")
Alternatively, install the development version from GitHub: ```r # install.packages("devtools") devtools::install_github("egnha/dub")
Unpacking/multiple assignment appears in other languages (e.g., Python, JavaScript, Clojure). While R has no such feature, using a custom operator to do this has long been a folklore method. To my knowledge, the earliest implementation is by Gabor Grothendieck (2004), cf. list
in the gsubfn package.
If you need specialized forms of "destructured" assignment, I recommend the zeallot package by Nate Teetor.
MIT Copyright © 2018 Eugene Ha
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.