Title: | The Unpacking Dot Operator |
---|---|
Description: | Provides a `.` object which can be used for unpacking assignments. For example, `.[rows, columns] <- dim(cars)` could be used to pull the number of rows and number of columns from `dim(cars)` into individual variables `rows` and `columns` in a single step. |
Authors: | Kevin Ushey [aut, cre] |
Maintainer: | Kevin Ushey <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0.9000 |
Built: | 2024-10-25 05:17:08 UTC |
Source: | https://github.com/kevinushey/dotty |
Use 'dotty' to perform destructuring assignments. Please see the examples below for usages.
.
.
An object of class dotty
of length 0.
# extract number of rows, number of columns from mtcars .[nr, nc] <- dim(mtcars) c(nr, nc) # extract first, last element of vector .[first, .., last] <- c(1, 2, 3, 4, 5) c(first, last) # extract a value by name .[beta = beta] <- list(alpha = 1, beta = 2, gamma = 3) beta # unpack nested values .[x, .[y, .[z]]] <- list(1, list(2, list(3))) c(x, y, z) # split version components .[major, minor, patch] <- getRversion() c(major, minor, patch)
# extract number of rows, number of columns from mtcars .[nr, nc] <- dim(mtcars) c(nr, nc) # extract first, last element of vector .[first, .., last] <- c(1, 2, 3, 4, 5) c(first, last) # extract a value by name .[beta = beta] <- list(alpha = 1, beta = 2, gamma = 3) beta # unpack nested values .[x, .[y, .[z]]] <- list(1, list(2, list(3))) c(x, y, z) # split version components .[major, minor, patch] <- getRversion() c(major, minor, patch)
'destructure' is primarily used to help define how an object should be prepared, or transformed, prior to a destructuring assignment. This can be relevant for objects which have unique subsetting semantics – for example, [numeric_version] objects.
destructure(object)
destructure(object)
object |
An R object. |
Packages which would like to define special destructring semantics for certain object classes can implement methods for this class.
When using 'dotty' within an R package, you might see **NOTE**s during 'R CMD check' of the form:
dotify()
dotify()
“' N checking R code for possible problems (1.8s) <package>: no visible binding for global variable <variable> Undefined global functions or variables: <variable> “'
This occurs because the [codetools] package, which is used for static analysis of R code during 'R CMD check', does not recognize that e.g. '.[apple] <- 42' would create a variable called 'apple' in the current scope. Calling ‘dotty::dotify()' in your package’s '.onLoad()' will allow 'dotty' to patch 'codetools' in a way that will allow it to understand 'dotty' usages, and so prevent these 'R CMD check' notes from being emitted.