zeallot

Variable assignment with zeal! (or multiple, unpacking, and destructuring assignment in R)

https://github.com/r-lib/zeallot

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (16.4%) to scientific vocabulary

Keywords

destructuring-assignment multiple-assignment pattern-matching r unpacking-assignment
Last synced: 6 months ago · JSON representation

Repository

Variable assignment with zeal! (or multiple, unpacking, and destructuring assignment in R)

Basic Info
  • Host: GitHub
  • Owner: r-lib
  • License: other
  • Language: R
  • Default Branch: main
  • Homepage:
  • Size: 301 KB
Statistics
  • Stars: 263
  • Watchers: 8
  • Forks: 14
  • Open Issues: 1
  • Releases: 8
Topics
destructuring-assignment multiple-assignment pattern-matching r unpacking-assignment
Created about 9 years ago · Last pushed 9 months ago
Metadata Files
Readme Changelog License

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

library(zeallot)
```

# zeallot

Variable assignment with zeal!


[![R-CMD-check](https://github.com/r-lib/zeallot/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/zeallot/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/r-lib/zeallot/graph/badge.svg)](https://app.codecov.io/gh/r-lib/zeallot)
[![CRAN status](https://www.r-pkg.org/badges/version/zeallot)](https://CRAN.R-project.org/package=zeallot)


## What's there to be excited about?

zeallot allows multiple, unpacking, or destructuring assignment in R by
providing the `%<-%` operator. With zeallot you can tighten code with explicit
variable names, unpack pieces of a lengthy list or the entirety of a small list,
destructure and assign object elements, or do it all at once.

Unpack a list of values.

```{r}
c(x, y) %<-% list(2, 3)

x
y
```

Destructure a data frame and assign its columns.

```{r}
c(cyl=, wt=) %<-% mtcars

cyl
wt
```

Unpack a nested list into nested left-hand side variables.

```{r}
c(c(x, y), z) %<-% list(list(1, 2), 3)

x
y
z
```

Destructure and partially unpack a list. "a" is assigned to `first` and the
remaining letters are grouped and assigned to one variable.

```{r}
c(first, ..rest) %<-% letters

first
unlist(rest)
```

## Getting started

Below is a simple usage example using the
[purrr](https://github.com/tidyverse/purrr) package and the safely function.

### Safe functions

The `purrr::safely` function returns a "safe" version of a function. The
following example is borrowed from the safely documentation. In this example a
safe version of the log function is created.

```{r}
safe_log <- purrr::safely(log)

safe_log(10)

safe_log("a")
```

A safe function always returns a list of two elements and will not throw an
error. Instead of throwing an error, the error element of the return list is set
and the value element is NULL. When called successfully the result element is
set and the error element is NULL.

Safe functions are a great way to write self-documenting code. However, dealing
with a return value that is always a list could prove tedious and may undo
efforts to write concise, readable code. Enter zeallot.

### The `%<-%` operator

With zeallot's unpacking operator `%<-%` we can unpack a safe function's return
value into two explicit variables and avoid dealing with the list return value
all together.

```{r}
c(res, err) %<-% safe_log(10)

res
err
```

The name structure of the operator is a flat or nested set of bare variable
names built with calls to `c()`. . These variables do not need to be previously
defined. On the right-hand side is a vector, list, or other R object to unpack.
`%<-%` unpacks the right-hand side, checks the number of variable names against
the number of unpacked values, and then assigns each unpacked value to a
variable. The result, instead of dealing with a list of values there are two
distinct variables, `res` and `err`.

### Further reading and examples

For more on the above example, other examples, and a thorough introduction to
zeallot check out the vignette on [unpacking
assignment](vignettes/unpacking-assignment.Rmd).

Below are links to discussions about multiple, unpacking, and destructuring
assignment in R,

* https://stackoverflow.com/questions/7519790/assign-multiple-new-variables-on-lhs-in-a-single-line-in-r
* https://stackoverflow.com/questions/1826519/how-to-assign-from-a-function-which-returns-more-than-one-value

## Installation

You can install the development version of zeallot from
[GitHub](https://github.com/) with:

```{r, eval=FALSE}
# install.packages("pak")
pak::pak("r-lib/zeallot")
```

---

Thank you to Paul Teetor for inspiring me to build zeallot.

Without his encouragement nothing would have gotten off the ground.

Owner

  • Name: R infrastructure
  • Login: r-lib
  • Kind: organization

GitHub Events

Total
  • Create event: 2
  • Release event: 1
  • Issues event: 14
  • Watch event: 12
  • Delete event: 5
  • Issue comment event: 17
  • Push event: 19
  • Pull request event: 2
Last Year
  • Create event: 2
  • Release event: 1
  • Issues event: 14
  • Watch event: 12
  • Delete event: 5
  • Issue comment event: 17
  • Push event: 19
  • Pull request event: 2

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 173
  • Total Committers: 3
  • Avg Commits per committer: 57.667
  • Development Distribution Score (DDS): 0.179
Past Year
  • Commits: 26
  • Committers: 1
  • Avg Commits per committer: 26.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
nteetor n****r@g****m 142
nteetor n****e@h****h 26
Paul Teetor p****r@y****m 5
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 9 months ago

All Time
  • Total issues: 55
  • Total pull requests: 9
  • Average time to close issues: 7 months
  • Average time to close pull requests: 2 days
  • Total issue authors: 19
  • Total pull request authors: 3
  • Average comments per issue: 1.87
  • Average comments per pull request: 1.22
  • Merged pull requests: 8
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 4
  • Pull requests: 1
  • Average time to close issues: 3 months
  • Average time to close pull requests: 17 days
  • Issue authors: 2
  • Pull request authors: 1
  • Average comments per issue: 1.5
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • nteetor (26)
  • hadley (9)
  • t-kalinowski (4)
  • UnclAlDeveloper (1)
  • Sinha-Ujjawal (1)
  • chrsigg (1)
  • rafaqz (1)
  • choisy (1)
  • brooklynbagel (1)
  • egnha (1)
  • llrs (1)
  • kongdd (1)
  • seasmith (1)
  • daranzolin (1)
  • JosiahParry (1)
Pull Request Authors
  • nteetor (8)
  • dkahle (1)
  • pteetor (1)
Top Labels
Issue Labels
enhancement (3) bug (2)
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • cran 27,989 last-month
  • Total docker downloads: 733,860
  • Total dependent packages: 24
    (may contain duplicates)
  • Total dependent repositories: 213
    (may contain duplicates)
  • Total versions: 15
  • Total maintainers: 1
cran.r-project.org: zeallot

Multiple, Unpacking, and Destructuring Assignment

  • Versions: 7
  • Dependent Packages: 24
  • Dependent Repositories: 213
  • Downloads: 27,989 Last month
  • Docker Downloads: 733,860
Rankings
Dependent repos count: 1.2%
Stargazers count: 1.7%
Downloads: 2.3%
Dependent packages count: 3.0%
Average: 5.6%
Forks count: 5.8%
Docker downloads count: 19.8%
Maintainers (1)
Last synced: 6 months ago
proxy.golang.org: github.com/r-lib/zeallot
  • Versions: 8
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.5%
Average: 5.6%
Dependent repos count: 5.8%
Last synced: 7 months ago

Dependencies

DESCRIPTION cran
  • R >= 3.2 depends
  • knitr * suggests
  • magrittr * suggests
  • purrr * suggests
  • rmarkdown * suggests
  • testthat * suggests
.github/workflows/r-cmd-check.yml actions
  • actions/cache v1 composite
  • actions/checkout v2 composite
  • actions/upload-artifact master composite
  • r-lib/actions/setup-pandoc master composite
  • r-lib/actions/setup-r master composite