Skip to content

Commit 7c3d751

Browse files
committed
use air formatting
1 parent b036458 commit 7c3d751

26 files changed

Lines changed: 1847 additions & 1327 deletions

.Rbuildignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ paper.bib
2929
^_quarto\.yml$
3030
^\.quarto$
3131
^methodshub
32-
.quarto/
32+
.quarto/
33+
^[\.]?air\.toml$
34+
^\.vscode$

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"Posit.air-vscode"
4+
]
5+
}

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"[r]": {
3+
"editor.formatOnSave": true,
4+
"editor.defaultFormatter": "Posit.air-vscode"
5+
}
6+
}

R/annotate_functions.R

Lines changed: 88 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,41 @@
2222
#'
2323

2424
draw_circle <- function(col = "#00BFFF", use = "focus", max.circle) {
25-
if (!requireNamespace("ggplot2", quietly = TRUE)) {
26-
stop("ggplot2 needed for this function to work. Please install it.", call. = FALSE)
27-
}
28-
if (!use %in% c("focus", "cent")) {
29-
stop("use must be one of 'focus' or 'cent'")
30-
}
31-
if (use == "focus" && missing(max.circle)) {
32-
stop("max.circle missing. Should be set to the max distance from focal node.")
25+
if (!requireNamespace("ggplot2", quietly = TRUE)) {
26+
stop(
27+
"ggplot2 needed for this function to work. Please install it.",
28+
call. = FALSE
29+
)
30+
}
31+
if (!use %in% c("focus", "cent")) {
32+
stop("use must be one of 'focus' or 'cent'")
33+
}
34+
if (use == "focus" && missing(max.circle)) {
35+
stop(
36+
"max.circle missing. Should be set to the max distance from focal node."
37+
)
38+
}
39+
dat <- data.frame()
40+
if (use == "cent") {
41+
for (d in seq(0, 100, 20) * 2) {
42+
tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = d))
43+
tmp[["grp"]] <- d
44+
dat <- rbind(dat, tmp)
3345
}
34-
dat <- data.frame()
35-
if (use == "cent") {
36-
for (d in seq(0, 100, 20) * 2) {
37-
tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = d))
38-
tmp[["grp"]] <- d
39-
dat <- rbind(dat, tmp)
40-
}
41-
} else if (use == "focus") {
42-
for (d in 1:max.circle) {
43-
tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = 2 * d))
44-
tmp[["grp"]] <- d
45-
dat <- rbind(dat, tmp)
46-
}
46+
} else if (use == "focus") {
47+
for (d in 1:max.circle) {
48+
tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = 2 * d))
49+
tmp[["grp"]] <- d
50+
dat <- rbind(dat, tmp)
4751
}
48-
circs <- ggplot2::geom_path(data = dat, ggplot2::aes_(x = ~x, y = ~y, group = ~grp), col = col, alpha = 0.5)
49-
return(circs)
52+
}
53+
circs <- ggplot2::geom_path(
54+
data = dat,
55+
ggplot2::aes_(x = ~x, y = ~y, group = ~grp),
56+
col = col,
57+
alpha = 0.5
58+
)
59+
return(circs)
5060
}
5161

5262

@@ -75,48 +85,68 @@ draw_circle <- function(col = "#00BFFF", use = "focus", max.circle) {
7585
#' }
7686
#' @export
7787
#'
78-
annotate_circle <- function(cent, col = "#00BFFF", format = "", pos = "top", text_size = 3) {
79-
if (!requireNamespace("ggplot2", quietly = TRUE)) {
80-
stop("ggplot2 needed for this function to work. Please install it.", call. = FALSE)
81-
}
82-
if (length(cent) == 1) {
83-
cent <- seq(1, cent, 1)
84-
dat_annot <- data.frame(y = seq(0, 100, 20), x = 0, val = interpolate_cent(cent, seq(0, 100, 20)))
85-
dat_annot[["val"]] <- round(dat_annot[["val"]], 8)
86-
} else {
87-
dat_annot <- data.frame(y = 100 - seq(0, 100, 20), x = 0, val = interpolate_cent(cent, seq(0, 100, 20)))
88-
dat_annot[["val"]] <- round(dat_annot[["val"]], 8)
89-
}
90-
vju <- 0
91-
if (format == "scientific") {
92-
dat_annot[["val"]] <- format(dat_annot[["val"]], scientific = TRUE)
93-
}
94-
if (pos == "bottom") {
95-
dat_annot[["y"]] <- -dat_annot[["y"]]
96-
vju <- 1
97-
}
98-
99-
circs <- ggplot2::geom_text(
100-
data = dat_annot, ggplot2::aes_(x = ~x, y = ~y, label = ~val),
101-
col = col, vjust = vju, size = text_size
88+
annotate_circle <- function(
89+
cent,
90+
col = "#00BFFF",
91+
format = "",
92+
pos = "top",
93+
text_size = 3
94+
) {
95+
if (!requireNamespace("ggplot2", quietly = TRUE)) {
96+
stop(
97+
"ggplot2 needed for this function to work. Please install it.",
98+
call. = FALSE
10299
)
103-
return(circs)
100+
}
101+
if (length(cent) == 1) {
102+
cent <- seq(1, cent, 1)
103+
dat_annot <- data.frame(
104+
y = seq(0, 100, 20),
105+
x = 0,
106+
val = interpolate_cent(cent, seq(0, 100, 20))
107+
)
108+
dat_annot[["val"]] <- round(dat_annot[["val"]], 8)
109+
} else {
110+
dat_annot <- data.frame(
111+
y = 100 - seq(0, 100, 20),
112+
x = 0,
113+
val = interpolate_cent(cent, seq(0, 100, 20))
114+
)
115+
dat_annot[["val"]] <- round(dat_annot[["val"]], 8)
116+
}
117+
vju <- 0
118+
if (format == "scientific") {
119+
dat_annot[["val"]] <- format(dat_annot[["val"]], scientific = TRUE)
120+
}
121+
if (pos == "bottom") {
122+
dat_annot[["y"]] <- -dat_annot[["y"]]
123+
vju <- 1
124+
}
125+
126+
circs <- ggplot2::geom_text(
127+
data = dat_annot,
128+
ggplot2::aes_(x = ~x, y = ~y, label = ~val),
129+
col = col,
130+
vjust = vju,
131+
size = text_size
132+
)
133+
return(circs)
104134
}
105135

106136

107137
# helper -----
108138
circleFun <- function(center = c(0, 0), diameter = 1, npoints = 100) {
109-
r <- diameter / 2
110-
tt <- seq(0, 2 * pi, length.out = npoints)
111-
xx <- center[1] + r * cos(tt)
112-
yy <- center[2] + r * sin(tt)
113-
return(data.frame(x = xx, y = yy))
139+
r <- diameter / 2
140+
tt <- seq(0, 2 * pi, length.out = npoints)
141+
xx <- center[1] + r * cos(tt)
142+
yy <- center[2] + r * sin(tt)
143+
return(data.frame(x = xx, y = yy))
114144
}
115145

116146
interpolate_cent <- function(cent, x) {
117-
a <- min(cent)
118-
b <- max(cent)
119-
alpha <- 100 / (b - a)
120-
beta <- -100 / (b - a) * a
121-
(x - beta) / alpha
147+
a <- min(cent)
148+
b <- max(cent)
149+
alpha <- 100 / (b - a)
150+
beta <- -100 / (b - a) * a
151+
(x - beta) / alpha
122152
}

R/graph_manipulate.R

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ NULL
2323
#' @export
2424

2525
reorder_edges <- function(g, attr, desc = TRUE) {
26-
if (!"name" %in% igraph::vertex_attr_names(g)) {
27-
igraph::V(g)$name <- 1:igraph::vcount(g)
28-
}
29-
edges_df <- igraph::as_data_frame(g, what = "edges")
30-
edges_df <- edges_df[order(edges_df[[attr]], decreasing = desc), ]
26+
if (!"name" %in% igraph::vertex_attr_names(g)) {
27+
igraph::V(g)$name <- 1:igraph::vcount(g)
28+
}
29+
edges_df <- igraph::as_data_frame(g, what = "edges")
30+
edges_df <- edges_df[order(edges_df[[attr]], decreasing = desc), ]
3131

32-
vertices <- igraph::as_data_frame(g, what = "vertices")
33-
vattrs <- igraph::vertex_attr_names(g)
34-
idname <- which(vattrs == "name")
35-
vertices <- vertices[, c(idname, setdiff(seq_along(vattrs), idname))]
32+
vertices <- igraph::as_data_frame(g, what = "vertices")
33+
vattrs <- igraph::vertex_attr_names(g)
34+
idname <- which(vattrs == "name")
35+
vertices <- vertices[, c(idname, setdiff(seq_along(vattrs), idname))]
3636

37-
gn <- igraph::graph_from_data_frame(
38-
d = edges_df,
39-
directed = igraph::is_directed(g),
40-
vertices = vertices
41-
)
42-
gn
37+
gn <- igraph::graph_from_data_frame(
38+
d = edges_df,
39+
directed = igraph::is_directed(g),
40+
vertices = vertices
41+
)
42+
gn
4343
}

0 commit comments

Comments
 (0)