-
Notifications
You must be signed in to change notification settings - Fork 276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strict pluck #482 #522
Strict pluck #482 #522
Changes from 17 commits
d19e803
ae33c46
ab4e5b8
0ce0ea8
08f89b3
116a700
38b574e
feed03f
4027eb6
1e9d00f
210e670
7dd29d8
c0384cf
50f5ba9
68ad335
344bbba
94572ed
8b124ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,24 @@ | ||
#' Pluck out a single element from a vector or environment | ||
#' Pluck or chuck a single element from a vector or environment | ||
#' | ||
#' @description | ||
#' | ||
#' This is a generalised form of `[[` which allows you to index deeply | ||
#' and flexibly into data structures. It supports R standard accessors | ||
#' like integer positions and string names, and also accepts arbitrary | ||
#' accessor functions, i.e. functions that take an object and return | ||
#' some internal piece. | ||
#' `pluck()` and `chuck()` implement a generalised form of `[[` that | ||
#' allow you to index deeply and flexibly into data structures. While | ||
#' `pluck()` consistently returns `NULL` when an element does not | ||
#' exist, `chuck()` always throws an error in that case. | ||
#' | ||
#' `pluck()` is often more readable than a mix of operators and | ||
#' accessors because it reads linearly and is free of syntactic | ||
#' cruft. Compare: \code{accessor(x[[1]])$foo} to `pluck(x, 1, | ||
#' accessor, "foo")`. | ||
#' * You can pluck or chuck with standard accessors like integer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe these should move down to details? |
||
#' positions and string names, and also accepts arbitrary accessor | ||
#' functions, i.e. functions that take an object and return some | ||
#' internal piece. | ||
#' | ||
#' Furthermore, `pluck()` never partial-matches unlike `$` which will | ||
#' select the `disp` object if you write `mtcars$di`. | ||
#' This is often more readable than a mix of operators and accessors | ||
#' because it reads linearly and is free of syntactic | ||
#' cruft. Compare: \code{accessor(x[[1]])$foo} to `pluck(x, 1, | ||
#' accessor, "foo")`. | ||
#' | ||
#' `attr_getter()` generates an attribute accessor function; | ||
#' i.e., it generates a function for extracting an attribute with | ||
#' a given name. Unlike the base R `attr()` function with default | ||
#' options, it doesn't use partial matching. | ||
#' | ||
#' @details | ||
#' | ||
#' Since it handles arbitrary accessor functions, `pluck()` is a type | ||
#' of composition operator. However, it is indexing-oriented thanks to | ||
#' its handling of strings and integers. By the same token is also | ||
#' explicit regarding the intent of the composition (e.g. extraction). | ||
#' * These accessors never partial-match. This is unlike `$` which | ||
#' will select the `disp` object if you write `mtcars$di`. | ||
#' | ||
#' @param .x A vector or environment | ||
#' @param ... A list of accessors for indexing into the object. Can be | ||
|
@@ -38,8 +30,16 @@ | |
#' particular, if your accessors are stored in a list, you can | ||
#' splice that in with `!!!`. | ||
#' @param .default Value to use if target is empty or absent. | ||
#' @keywords internal | ||
#' @export | ||
#' | ||
#' @details | ||
#' | ||
#' Since it handles arbitrary accessor functions, `pluck()` is a type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this paragraph. Maybe it can be removed? |
||
#' of composition operator. However, it is indexing-oriented thanks to | ||
#' its handling of strings and integers. By the same token is also | ||
#' explicit regarding the intent of the composition (e.g. extraction). | ||
#' | ||
#' @seealso [attr_getter()] for creating attribute getters suitable | ||
#' for use with `pluck()` and `chuck()`. | ||
#' @examples | ||
#' # pluck() supports integer positions, string names, and functions. | ||
#' # Using functions, you can easily extend pluck(). Let's create a | ||
|
@@ -61,30 +61,57 @@ | |
#' # expression: | ||
#' my_element(x[[1]]) | ||
#' | ||
#' # If you have a list of accessors, you can splice those in with `!!!`: | ||
#' idx <- list(1, my_element) | ||
#' pluck(x, !!!idx) | ||
#' @export | ||
pluck <- function(.x, ..., .default = NULL) { | ||
.Call( | ||
pluck_impl, | ||
x = .x, | ||
index = list2(...), | ||
missing = .default, | ||
strict = FALSE | ||
) | ||
} | ||
#' @rdname pluck | ||
#' @export | ||
chuck <- function(.x, ...) { | ||
.Call( | ||
pluck_impl, | ||
x = .x, | ||
index = list2(...), | ||
missing = NULL, | ||
strict = TRUE | ||
) | ||
} | ||
|
||
#' Create an attribute getter function | ||
#' | ||
#' `attr_getter()` generates an attribute accessor function; i.e., it | ||
#' generates a function for extracting an attribute with a given | ||
#' name. Unlike the base R `attr()` function with default options, it | ||
#' doesn't use partial matching. | ||
#' | ||
#' # This technique is used for plucking into attributes with | ||
#' # attr_getter(). It takes an attribute name and returns a function | ||
#' # to access the attribute: | ||
#' @param attr An attribute name as string. | ||
#' | ||
#' @seealso [pluck()] | ||
#' @examples | ||
#' # attr_getter() takes an attribute name and returns a function to | ||
#' # access the attribute: | ||
#' get_rownames <- attr_getter("row.names") | ||
#' get_rownames(mtcars) | ||
#' | ||
#' # These getter functions are handy in conjunction with pluck() for | ||
#' # extracting deeply into a data structure. Here we'll first | ||
#' # extract by position, then by attribute: | ||
#' obj1 <- structure("obj", obj_attr = "foo") | ||
#' obj2 <- structure("obj", obj_attr = "bar") | ||
#' x <- list(obj1, obj2) | ||
#' | ||
#' # pluck() is handy for extracting deeply into a data structure. | ||
#' # Here we'll first extract by position, then by attribute: | ||
#' pluck(x, 1, attr_getter("obj_attr")) # From first object | ||
#' pluck(x, 2, attr_getter("obj_attr")) # From second object | ||
#' | ||
#' | ||
#' # If you have a list of accessors, you can splice those in with `!!!`: | ||
#' idx <- list(1, attr_getter("obj_attr")) | ||
#' pluck(x, !!!idx) | ||
pluck <- function(.x, ..., .default = NULL) { | ||
.Call(extract_impl, .x, list2(...), .default) | ||
} | ||
|
||
#' @export | ||
#' @rdname pluck | ||
#' @param attr An attribute name as string. | ||
attr_getter <- function(attr) { | ||
force(attr) | ||
function(x) attr(x, attr, exact = TRUE) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove "while"