|
a |
|
b/R/utils-pipe.R |
|
|
1 |
#' Pipe operator |
|
|
2 |
#' |
|
|
3 |
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. |
|
|
4 |
#' |
|
|
5 |
#' @name %>% |
|
|
6 |
#' @rdname pipe |
|
|
7 |
#' @keywords internal |
|
|
8 |
#' @export |
|
|
9 |
#' @importFrom magrittr %>% |
|
|
10 |
#' @usage lhs \%>\% rhs |
|
|
11 |
#' @param lhs A value or the magrittr placeholder. |
|
|
12 |
#' @param rhs A function call using the magrittr semantics. |
|
|
13 |
#' @return The result of calling `rhs(lhs)`. |
|
|
14 |
NULL |
|
|
15 |
|
|
|
16 |
#' Assignment pipe |
|
|
17 |
#' |
|
|
18 |
#' See \code{magrittr::\link[magrittr:pipe]{\%<>\%}} for details. |
|
|
19 |
#' |
|
|
20 |
#' @name %<>% |
|
|
21 |
#' @keywords internal |
|
|
22 |
#' @export |
|
|
23 |
#' @importFrom magrittr %<>% |
|
|
24 |
#' @usage lhs \%<>\% rhs |
|
|
25 |
#' @param lhs A value or the magrittr placeholder. |
|
|
26 |
#' @param rhs A function call using the magrittr semantics. |
|
|
27 |
#' @return The result of calling `rhs(lhs)`. |
|
|
28 |
NULL |
|
|
29 |
|
|
|
30 |
# flag determined by the correlation table from one table or two tables |
|
|
31 |
t_flag <- \(corr){ |
|
|
32 |
if (!nrow(corr) == ncol(corr)) { |
|
|
33 |
return(FALSE) |
|
|
34 |
} |
|
|
35 |
if (!all(rownames(corr) == colnames(corr))) { |
|
|
36 |
return(FALSE) |
|
|
37 |
} |
|
|
38 |
return(TRUE) |
|
|
39 |
} |
|
|
40 |
|
|
|
41 |
# 多列表是否一一对应 |
|
|
42 |
# test 对应的列需要跟剩下的列具有唯一映射 |
|
|
43 |
e_match <- function(df, test = NULL) { |
|
|
44 |
dis_df <- dplyr::distinct_all(df) %>% as.data.frame() |
|
|
45 |
if (is.null(test)) { |
|
|
46 |
return(all_same(c(nrow(dis_df), apply(dis_df, 2, \(i)length(unique(i)))))) |
|
|
47 |
} else { |
|
|
48 |
return(all_same(c(nrow(dis_df), nrow(dplyr::distinct_all(dis_df[, test, drop = FALSE]))))) |
|
|
49 |
} |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
all_same <- \(x){ |
|
|
53 |
return(all(x == x[1])) |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
# choose the last not na value |
|
|
57 |
condance <- \(aa){ |
|
|
58 |
aa <- as.data.frame(aa) |
|
|
59 |
if (any(is.na(aa[, length(aa)]))) { |
|
|
60 |
res <- apply(aa, 1, \(x){ |
|
|
61 |
tmp <- x[!is.na(x)] |
|
|
62 |
if (length(tmp) == 0) { |
|
|
63 |
return(NA) |
|
|
64 |
} |
|
|
65 |
return(tmp[length(tmp)]) |
|
|
66 |
}) |
|
|
67 |
} else { |
|
|
68 |
res <- aa[, length(aa), drop = TRUE] |
|
|
69 |
} |
|
|
70 |
res |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
# bind two df with same columns, the last df will replace first df |
|
|
74 |
cbind_new <- \(df, df1){ |
|
|
75 |
if (ncol(df) < 1) { |
|
|
76 |
return(df1) |
|
|
77 |
} |
|
|
78 |
if (ncol(df1) < 1) { |
|
|
79 |
return(df) |
|
|
80 |
} |
|
|
81 |
inter <- intersect(colnames(df1), colnames(df)) |
|
|
82 |
la <- setdiff(colnames(df), inter) |
|
|
83 |
cbind(df[, la, drop = FALSE], df1) |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
twocol2vector <- \(df){ |
|
|
87 |
if (ncol(df) < 2) { |
|
|
88 |
return(NULL) |
|
|
89 |
} |
|
|
90 |
if (!e_match(df, test = 1)) { |
|
|
91 |
stop("The columns are not one-to-one correspondence.") |
|
|
92 |
} |
|
|
93 |
df <- dplyr::distinct_all(df) |
|
|
94 |
setNames(df[, 2], df[, 1]) |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
custom_sort <- function(x) { |
|
|
98 |
# 尝试将字符转换为数字并排序 |
|
|
99 |
converted <- suppressWarnings(as.numeric(x)) |
|
|
100 |
if (!any(is.na(converted))) { |
|
|
101 |
# 如果转换成功,则返回按数字排序的结果 |
|
|
102 |
return(converted[order(converted)]) |
|
|
103 |
} else { |
|
|
104 |
# 如果转换失败,则返回按字符排序的结果 |
|
|
105 |
return(stringr::str_sort(x, numeric = TRUE)) |
|
|
106 |
} |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
paste_df <- function(df, collapse = ",") { |
|
|
110 |
apply(df, 1, paste, collapse = collapse) |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
paste_df2pielist <- function(df, collapse = ",") { |
|
|
114 |
pcutils::strsplit2(df, split = collapse) %>% |
|
|
115 |
pcutils::t2() %>% |
|
|
116 |
as.list() %>% |
|
|
117 |
lapply(function(x) as.numeric(x)) |
|
|
118 |
} |
|
|
119 |
|
|
|
120 |
deprecated <- function(old, new) { |
|
|
121 |
assign(old, new, envir = asNamespace(packageName())) |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
#' @export c_net_cal |
|
|
125 |
deprecated("c_net_cal", c_net_calculate) |
|
|
126 |
|
|
|
127 |
#' @export c_net_module |
|
|
128 |
deprecated("c_net_module", module_detect) |
|
|
129 |
|
|
|
130 |
#' @export c_net_lay |
|
|
131 |
deprecated("c_net_lay", c_net_layout) |
|
|
132 |
|
|
|
133 |
#' @export as.metanet |
|
|
134 |
deprecated("as.metanet", c_net_update) |