Skip to content

Commit

Permalink
Getter functions now evaluate ... with tidy evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Jan 12, 2024
1 parent 3a7213f commit 270f3ce
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
this update adds the option to use labels instead of an arrow using the
`labels` argument.
* `create_layout()` now returns a modified tibble rather than a data.frame
* The `...` in `get_nodes()`, `get_edges()`, and `get_con()` now accepts
tidy evaluation. `get_nodes()` and `get_con()` will get evaluated on the node
data in the original order (layouts may reorder nodes), and `get_edges()` will
get evaluated on the edge data (#272)

# ggraph 2.1.0

Expand Down
11 changes: 8 additions & 3 deletions R/connections.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#' @param paths A list of integer vectors giving the index of nodes defining
#' connections
#'
#' @param ... Additional information to be added to the final data output
#' @param ... Additional information to be added to the final data output.
#' Accepts expressions that will be evaluated on the node data in it's
#' original order (irrespective of any reordering by the layout)
#'
#' @param weight An expression to be evaluated on the edge data to provide
#' weights for the shortest path calculations
Expand All @@ -33,6 +35,7 @@ get_con <- function(from = integer(), to = integer(), paths = NULL, ..., weight
if (length(from) != length(to)) {
cli::cli_abort('{.arg from} and {.arg to} must be of equal length')
}
dots <- enquos(...)
function(layout) {
if (length(from) == 0) {
return(NULL)
Expand All @@ -55,8 +58,10 @@ get_con <- function(from = integer(), to = integer(), paths = NULL, ..., weight
)
nodes <- vec_rbind(nodes, extra)
}
extra_data <- lapply(list2(...), function(x) {
rep(x, length.out = length(from))[nodes$con.id]
layout <- layout[order(layout$.ggraph.orig_index), ]
extra_data <- lapply(dots, function(x) {
val <- eval_tidy(x, layout)
rep(val, length.out = length(from))[nodes$con.id]
})
if (length(extra_data) > 0) {
nodes <- cbind(
Expand Down
12 changes: 8 additions & 4 deletions R/edges.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#' details for more information
#'
#' @param ... Additional data that will be cbind'ed together with the returned
#' edge data.
#' edge data. Accepts expressions that will be evaluated on the edge data
#'
#' @return A data.frame with columns dependent on format as well as the graph
#' type. In addition to the columns discussed in the details section,
Expand All @@ -74,9 +74,10 @@
#'
get_edges <- function(format = 'short', collapse = 'none', ...) {
if (!collapse %in% c('none', 'all', 'direction')) {
cli::cli_abort('{.arg ollapse} must be either {.val none}, {.val all} or {.val direction}')
cli::cli_abort('{.arg collapse} must be either {.val none}, {.val all} or {.val direction}')
}
function(layout) {
dots <- enquos(...)
function(layout) {browser()
edges <- collect_edges(layout)
edges <- switch(
collapse,
Expand All @@ -90,7 +91,10 @@ get_edges <- function(format = 'short', collapse = 'none', ...) {
long = format_long_edges(edges, layout),
cli::cli_abort('Unknown {.arg format}. Use either {.val short} or {.val long}')
)
extra_data <- lapply(list2(...), rep, length.out = nrow(edges))
extra_data <- lapply(dots, function(x) {
val <- eval_tidy(x, edges)
rep(val, length.out = nrow(edges))
})
if (length(extra_data) > 0) {
edges <- cbind(
edges,
Expand Down
10 changes: 8 additions & 2 deletions R/nodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#' node layers.
#'
#' @param ... Additional data that should be cbind'ed together with the node
#' data.
#' data. Accepts expressions that will be evaluated on the node data in it's
#' original order (irrespective of any reordering by the layout)
#'
#' @return A data.frame with the node data as well of any additional data
#' supplied through `...`
Expand All @@ -20,8 +21,13 @@
#' @export
#'
get_nodes <- function(...) {
dots <- enquos(...)
function(layout) {
extra_data <- lapply(list2(...), rep, length.out = nrow(layout))
layout_reorder <- layout[order(layout$.ggraph.orig_index), ]
extra_data <- lapply(dots, function(x) {
val <- eval_tidy(x, layout_reorder)
rep(val, length.out = nrow(layout))[layout$.ggraph.orig_index]
})
if (length(extra_data) > 0) {
layout <- cbind(
layout,
Expand Down

0 comments on commit 270f3ce

Please sign in to comment.