Skip to content

Commit

Permalink
use native pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Jan 30, 2024
1 parent 07dbdd4 commit 09ae5ac
Show file tree
Hide file tree
Showing 50 changed files with 256 additions and 158 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ import(tidygraph)
import(vctrs)
importFrom(MASS,bandwidth.nrd)
importFrom(MASS,kde2d)
importFrom(dplyr,"%>%")
importFrom(dplyr,arrange)
importFrom(dplyr,do)
importFrom(dplyr,group_by)
Expand Down Expand Up @@ -358,7 +357,6 @@ importFrom(igraph,delete_vertex_attr)
importFrom(igraph,distances)
importFrom(igraph,edge_attr)
importFrom(igraph,ends)
importFrom(igraph,get.edge.ids)
importFrom(igraph,gorder)
importFrom(igraph,graph_attr)
importFrom(igraph,gsize)
Expand Down
2 changes: 1 addition & 1 deletion R/autograph.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#'
#' @examples
#' library(tidygraph)
#' gr <- create_notable('herschel') %>%
#' gr <- create_notable('herschel') |>
#' mutate(class = sample(letters[1:3], n(), TRUE)) %E>%
#' mutate(weight = runif(n()))
#'
Expand Down
16 changes: 8 additions & 8 deletions R/edges.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,28 @@ expand_edge_aes <- function(x) {
}
x
}
#' @importFrom dplyr %>% group_by top_n ungroup
#' @importFrom dplyr group_by top_n ungroup
collapse_all_edges <- function(edges) {
from <- pmin(edges$from, edges$to)
to <- pmax(edges$to, edges$from)
id <- paste(from, to, sep = '-')
if (anyDuplicated(id)) {
edges$.id <- id
edges <- edges %>%
group_by(.data$.id) %>%
top_n(1) %>%
edges <- edges |>
group_by(.data$.id) |>
top_n(1) |>
ungroup()
}
data_frame0(edges)
}
#' @importFrom dplyr %>% group_by top_n ungroup
#' @importFrom dplyr group_by top_n ungroup
collapse_dir_edges <- function(edges) {
id <- paste(edges$from, edges$to, sep = '-')
if (anyDuplicated(id)) {
edges$.id <- id
edges <- edges %>%
group_by(.data$.id) %>%
top_n(1) %>%
edges <- edges |>
group_by(.data$.id) |>
top_n(1) |>
ungroup()
}
data_frame0(edges)
Expand Down
2 changes: 1 addition & 1 deletion R/facet_graph.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#'
#' @examples
#' library(tidygraph)
#' gr <- as_tbl_graph(highschool) %>%
#' gr <- as_tbl_graph(highschool) |>
#' mutate(popularity = as.character(cut(centrality_degree(mode = 'in'),
#' breaks = 3,
#' labels = c('low', 'medium', 'high')
Expand Down
2 changes: 1 addition & 1 deletion R/facet_nodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#'
#' @examples
#' library(tidygraph)
#' gr <- as_tbl_graph(highschool) %>%
#' gr <- as_tbl_graph(highschool) |>
#' mutate(popularity = as.character(cut(centrality_degree(mode = 'in'),
#' breaks = 3,
#' labels = c('low', 'medium', 'high')
Expand Down
22 changes: 11 additions & 11 deletions R/geom_axis_hive.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#' @rdname ggraph-extensions
#' @format NULL
#' @usage NULL
#' @importFrom dplyr %>% group_by mutate slice ungroup
#' @importFrom dplyr group_by mutate slice ungroup
#' @export
StatAxisHive <- ggproto('StatAxisHive', StatFilter,
setup_data = function(data, params) {
data <- data %>%
group_by(.data$angle, .data$section, .data$PANEL) %>%
data <- data |>
group_by(.data$angle, .data$section, .data$PANEL) |>
mutate(
x = min(.data$r) * cos(.data$angle[1]) * 1.1,
y = min(.data$r) * sin(.data$angle[1]) * 1.1,
xend = max(.data$r) * cos(.data$angle[1]) * 1.1,
yend = max(.data$r) * sin(.data$angle[1]) * 1.1,
max_r = max(.data$r),
min_r = min(.data$r)
) %>%
slice(1) %>%
) |>
slice(1) |>
ungroup()
data_frame0(data)
},
Expand All @@ -26,7 +26,7 @@ StatAxisHive <- ggproto('StatAxisHive', StatFilter,
#' @format NULL
#' @usage NULL
#' @importFrom grid textGrob nullGrob
#' @importFrom dplyr %>% group_by summarise
#' @importFrom dplyr group_by summarise
#' @export
GeomAxisHive <- ggproto('GeomAxisHive', GeomSegment,
draw_panel = function(data, panel_scales, coord, label = TRUE, axis = TRUE, label_colour = 'black') {
Expand All @@ -35,8 +35,8 @@ GeomAxisHive <- ggproto('GeomAxisHive', GeomSegment,
data$xend <- data$xend / 1.1
data$yend <- data$yend / 1.1
data <- coord$transform(data, panel_scales)
label_data <- data %>%
group_by(.data$axis) %>%
label_data <- data |>
group_by(.data$axis) |>
summarise(
x = max(.data$max_r) * cos(mean(.data$angle)),
y = max(.data$max_r) * sin(mean(.data$angle)),
Expand Down Expand Up @@ -132,15 +132,15 @@ GeomAxisHive <- ggproto('GeomAxisHive', GeomSegment,
#' @examples
#' # Plot the flare import graph as a hive plot
#' library(tidygraph)
#' flareGr <- as_tbl_graph(flare$imports) %>%
#' flareGr <- as_tbl_graph(flare$imports) |>
#' mutate(
#' type = dplyr::case_when(
#' centrality_degree(mode = 'in') == 0 ~ 'Source',
#' centrality_degree(mode = 'out') == 0 ~ 'Sink',
#' TRUE ~ 'Both'
#' )
#' ) %>%
#' activate(edges) %>%
#' ) |>
#' activate(edges) |>
#' mutate(
#' type = dplyr::case_when(
#' grepl('flare.analytics', paste(.N()$name[from], .N()$name[to])) ~ 'Analytics',
Expand Down
2 changes: 1 addition & 1 deletion R/geom_conn_bundle.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#' @examples
#' # Create a graph of the flare class system
#' library(tidygraph)
#' flareGraph <- tbl_graph(flare$vertices, flare$edges) %>%
#' flareGraph <- tbl_graph(flare$vertices, flare$edges) |>
#' mutate(
#' class = map_bfs_chr(node_is_root(), .f = function(node, dist, path, ...) {
#' if (dist <= 1) {
Expand Down
2 changes: 1 addition & 1 deletion R/geom_edge.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' @format NULL
#' @usage NULL
#' @importFrom grid segmentsGrob polylineGrob gpar
#' @importFrom dplyr %>% group_by_ do ungroup
#' @importFrom dplyr group_by_ do ungroup
#' @importFrom ggforce interpolateDataFrame
#' @export
GeomEdgePath <- ggproto('GeomEdgePath', GeomPath,
Expand Down
10 changes: 5 additions & 5 deletions R/geom_edge_arc.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@
#' @examples
#' require(tidygraph)
#' # Make a graph with different directions of edges
#' gr <- create_notable('Meredith') %>%
#' convert(to_directed) %>%
#' mutate(class = sample(letters[1:3], n(), replace = TRUE)) %>%
#' activate(edges) %>%
#' gr <- create_notable('Meredith') |>
#' convert(to_directed) |>
#' mutate(class = sample(letters[1:3], n(), replace = TRUE)) |>
#' activate(edges) |>
#' mutate(
#' class = sample(letters[1:3], n(), replace = TRUE),
#' switch = sample(c(TRUE, FALSE), n(), replace = TRUE)
#' ) %>%
#' ) |>
#' reroute(from = to, to = from, subset = switch)
#'
#' ggraph(gr, 'linear') +
Expand Down
6 changes: 3 additions & 3 deletions R/geom_edge_bend.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@
#'
#' @examples
#' require(tidygraph)
#' gr <- create_tree(20, 4) %>%
#' mutate(class = sample(letters[1:3], n(), replace = TRUE)) %>%
#' activate(edges) %>%
#' gr <- create_tree(20, 4) |>
#' mutate(class = sample(letters[1:3], n(), replace = TRUE)) |>
#' activate(edges) |>
#' mutate(class = sample(letters[1:3], n(), replace = TRUE))
#'
#' ggraph(gr, 'tree') +
Expand Down
20 changes: 17 additions & 3 deletions R/geom_edge_bundle_path.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ geom_edge_bundle_path0 <- function(mapping = NULL, data = get_edges(),
)
)
}
#' @importFrom igraph gsize delete_edges shortest_paths get.edge.ids is_directed
#' @importFrom igraph gsize delete_edges shortest_paths is_directed as_edgelist
path_bundle <- function(graph, nodes, from, to, directed = directed, max_distortion = 2, weight_fac = 2) {
m <- gsize(graph)
lock <- rep(FALSE, m)
Expand All @@ -294,6 +294,13 @@ path_bundle <- function(graph, nodes, from, to, directed = directed, max_distort
cli::cli_warn("Ignoring {.arg directed} for undirected graphs")
}
mode <- if (is.null(directed) || !directed) "all" else "out"

all_edges <- as_edgelist(graph)
if (directed) {
all_edges <- paste(all_edges[,1], "-", all_edges[,2])
} else {
all_edges <- paste(pmin(all_edges[,1], all_edges[,2]), "-", pmax(all_edges[,1], all_edges[,2]))
}
# iterate
for (e in edges_order) {
s <- from[e]
Expand All @@ -315,8 +322,15 @@ path_bundle <- function(graph, nodes, from, to, directed = directed, max_distort
next()
}
all_edges_on_path <- rep(as.integer(path), each = 2)
all_edges_on_path <- all_edges_on_path[-c(1, length(all_edges_on_path))]
all_edges_on_path <- get.edge.ids(graph, all_edges_on_path, directed = mode != "all", error = FALSE, multi = TRUE)
all_edges_on_path <- matrix(all_edges_on_path[-c(1, length(all_edges_on_path))], ncol = 2, byrow = TRUE)
if (directed) {
all_edges_on_path <- paste(all_edges_on_path[,1], "-", all_edges_on_path[,2])
} else {
all_edges_on_path <- paste(pmin(all_edges_on_path[,1], all_edges_on_path[,2]),
"-",
pmax(all_edges_on_path[,1], all_edges_on_path[,2]))
}
all_edges_on_path <- all_edges %in% all_edges_on_path
lock[all_edges_on_path] <- TRUE
paths[[e]] <- path
}
Expand Down
4 changes: 2 additions & 2 deletions R/geom_edge_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
#'
#' @examples
#' require(tidygraph)
#' gr <- create_notable('bull') %>%
#' activate(edges) %>%
#' gr <- create_notable('bull') |>
#' activate(edges) |>
#' mutate(class = sample(letters[1:3], n(), replace = TRUE))
#'
#' ggraph(gr, 'stress') +
Expand Down
6 changes: 3 additions & 3 deletions R/geom_edge_diagonal.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@
#'
#' @examples
#' require(tidygraph)
#' gr <- create_tree(20, 4) %>%
#' mutate(class = sample(letters[1:3], n(), replace = TRUE)) %>%
#' activate(edges) %>%
#' gr <- create_tree(20, 4) |>
#' mutate(class = sample(letters[1:3], n(), replace = TRUE)) |>
#' activate(edges) |>
#' mutate(class = sample(letters[1:3], n(), replace = TRUE))
#'
#' ggraph(gr, 'tree') +
Expand Down
8 changes: 4 additions & 4 deletions R/geom_edge_elbow.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@
#'
#' @examples
#' require(tidygraph)
#' irisDen <- hclust(dist(iris[1:4], method = 'euclidean'), method = 'ward.D2') %>%
#' as_tbl_graph() %>%
#' mutate(class = sample(letters[1:3], n(), TRUE)) %>%
#' activate(edges) %>%
#' irisDen <- hclust(dist(iris[1:4], method = 'euclidean'), method = 'ward.D2') |>
#' as_tbl_graph() |>
#' mutate(class = sample(letters[1:3], n(), TRUE)) |>
#' activate(edges) |>
#' mutate(class = sample(letters[1:3], n(), TRUE))
#'
#' ggraph(irisDen, 'dendrogram', circular = TRUE) +
Expand Down
20 changes: 10 additions & 10 deletions R/geom_edge_fan.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
#'
#' @examples
#' require(tidygraph)
#' gr <- create_notable('bull') %>%
#' convert(to_directed) %>%
#' gr <- create_notable('bull') |>
#' convert(to_directed) |>
#' bind_edges(data.frame(from = c(1, 2, 2, 3), to = c(2, 1, 3, 2))) %E>%
#' mutate(class = sample(letters[1:3], 9, TRUE)) %N>%
#' mutate(class = sample(c('x', 'y'), 5, TRUE))
Expand Down Expand Up @@ -250,17 +250,17 @@ geom_edge_fan0 <- function(mapping = NULL, data = get_edges(),
)
)
}
#' @importFrom dplyr %>% group_by arrange mutate n ungroup transmute
#' @importFrom dplyr group_by arrange mutate n ungroup transmute
create_fans <- function(from, to, params) {
from$.id <- paste(pmin(from$from, to$to), pmax(from$from, to$to), sep = '-')
from$.orig_ind <- seq_len(nrow(from))
position <- from %>%
group_by(.data$PANEL, .data$.id) %>%
arrange(.data$from) %>%
mutate(position = seq_len(n()) - 0.5 - n() / 2) %>%
mutate(position = .data$position * ifelse(.data$from < .data$to, 1, -1)) %>%
ungroup() %>%
arrange(.data$.orig_ind) %>%
position <- from |>
group_by(.data$PANEL, .data$.id) |>
arrange(.data$from) |>
mutate(position = seq_len(n()) - 0.5 - n() / 2) |>
mutate(position = .data$position * ifelse(.data$from < .data$to, 1, -1)) |>
ungroup() |>
arrange(.data$.orig_ind) |>
transmute(position = .data$position)
position <- position$position
max_fans <- max(table(from$.id))
Expand Down
6 changes: 3 additions & 3 deletions R/geom_edge_hive.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@
#' @examples
#' # Plot the flare import graph as a hive plot
#' library(tidygraph)
#' flareGr <- as_tbl_graph(flare$imports) %>%
#' flareGr <- as_tbl_graph(flare$imports) |>
#' mutate(
#' type = dplyr::case_when(
#' centrality_degree(mode = 'in') == 0 ~ 'Source',
#' centrality_degree(mode = 'out') == 0 ~ 'Sink',
#' TRUE ~ 'Both'
#' )
#' ) %>%
#' activate(edges) %>%
#' ) |>
#' activate(edges) |>
#' mutate(
#' type = dplyr::case_when(
#' grepl('flare.analytics', paste(.N()$name[from], .N()$name[to])) ~ 'Analytics',
Expand Down
6 changes: 3 additions & 3 deletions R/geom_edge_link.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@
#'
#' @examples
#' require(tidygraph)
#' gr <- create_notable('bull') %>%
#' mutate(class = sample(letters[1:3], n(), replace = TRUE)) %>%
#' activate(edges) %>%
#' gr <- create_notable('bull') |>
#' mutate(class = sample(letters[1:3], n(), replace = TRUE)) |>
#' activate(edges) |>
#' mutate(class = sample(letters[1:3], n(), replace = TRUE))
#'
#' ggraph(gr, 'stress') +
Expand Down
20 changes: 10 additions & 10 deletions R/geom_edge_parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
#'
#' @examples
#' require(tidygraph)
#' gr <- create_notable('bull') %>%
#' convert(to_directed) %>%
#' gr <- create_notable('bull') |>
#' convert(to_directed) |>
#' bind_edges(data.frame(from = c(1, 2, 2, 3), to = c(2, 1, 3, 2))) %E>%
#' mutate(class = sample(letters[1:3], 9, TRUE)) %N>%
#' mutate(class = sample(c('x', 'y'), 5, TRUE))
Expand Down Expand Up @@ -230,16 +230,16 @@ geom_edge_parallel0 <- function(mapping = NULL, data = get_edges(),
)
)
}
#' @importFrom dplyr %>% group_by arrange summarise n ungroup pull
#' @importFrom dplyr group_by arrange summarise n ungroup pull
edge_positions <- function(from, to, params) {
from$.id <- paste(pmin(from$from, to$to), pmax(from$from, to$to), sep = '-')
from$.orig_ind <- seq_len(nrow(from))
from %>%
group_by(.data$PANEL, .data$.id) %>%
arrange(.data$from) %>%
mutate(position = seq_len(n()) - 0.5 - n() / 2) %>%
mutate(position = .data$position * ifelse(.data$from < .data$to, 1, -1)) %>%
ungroup() %>%
arrange(.data$.orig_ind) %>%
from |>
group_by(.data$PANEL, .data$.id) |>
arrange(.data$from) |>
mutate(position = seq_len(n()) - 0.5 - n() / 2) |>
mutate(position = .data$position * ifelse(.data$from < .data$to, 1, -1)) |>
ungroup() |>
arrange(.data$.orig_ind) |>
pull(.data$position)
}
10 changes: 5 additions & 5 deletions R/geom_edge_point.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
#'
#' @examples
#' require(tidygraph)
#' gr <- create_notable('zachary') %>%
#' mutate(group = group_infomap()) %>%
#' morph(to_split, group) %>%
#' activate(edges) %>%
#' mutate(edge_group = as.character(.N()$group[1])) %>%
#' gr <- create_notable('zachary') |>
#' mutate(group = group_infomap()) |>
#' morph(to_split, group) |>
#' activate(edges) |>
#' mutate(edge_group = as.character(.N()$group[1])) |>
#' unmorph()
#'
#' ggraph(gr, 'matrix', sort.by = node_rank_hclust()) +
Expand Down
Loading

0 comments on commit 09ae5ac

Please sign in to comment.