Skip to content

Commit f7b765b

Browse files
committed
add grouped_list
1 parent fec8fb4 commit f7b765b

File tree

11 files changed

+140
-38
lines changed

11 files changed

+140
-38
lines changed

NAMESPACE

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Generated by roxygen2: do not edit by hand
22

33
S3method("[[",indexed)
4+
S3method(export_fst,data.frame)
5+
S3method(export_fst,grouped_list)
46
S3method(print,data.table)
57
S3method(write_ufile,data.frame)
68
S3method(write_ufile,unit_df)
@@ -17,7 +19,6 @@ export(InitCluster)
1719
export(OS_type)
1820
export(SumatraPDF)
1921
export(add_dn)
20-
export(add_gridLine)
2122
export(add_group_id)
2223
export(addin_copyLines)
2324
export(addin_cutLines)
@@ -74,6 +75,7 @@ export(edit_r_profile)
7475
export(edit_r_profile_sys)
7576
export(evince)
7677
export(export)
78+
export(export_fst)
7779
export(file_cp)
7880
export(file_ext)
7981
export(file_mv)
@@ -100,10 +102,15 @@ export(github)
100102
export(glue)
101103
export(green)
102104
export(group_apply)
105+
export(group_by)
106+
export(group_map)
107+
export(group_map2)
108+
export(grouped_list)
103109
export(guess_names)
104110
export(icount)
105111
export(ifelse2)
106112
export(import)
113+
export(import_fst)
107114
export(install_git)
108115
export(install_gitee)
109116
export(install_github)
@@ -243,6 +250,9 @@ importFrom(dplyr,across)
243250
importFrom(dplyr,arrange)
244251
importFrom(dplyr,as_tibble)
245252
importFrom(dplyr,cur_group_id)
253+
importFrom(dplyr,group_by)
254+
importFrom(dplyr,group_keys)
255+
importFrom(dplyr,group_map)
246256
importFrom(dplyr,is_grouped_df)
247257
importFrom(dplyr,mutate)
248258
importFrom(dplyr,rename)
@@ -349,6 +359,7 @@ importFrom(stringr,str_extract_all)
349359
importFrom(stringr,str_replace_all)
350360
importFrom(stringr,str_trim)
351361
importFrom(sysfonts,font_add)
362+
importFrom(tibble,tibble)
352363
importFrom(usethis,edit_r_environ)
353364
importFrom(usethis,edit_r_profile)
354365
importFrom(usethis,use_build_ignore)
File renamed without changes.

R/main_dplyr.R

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
NULL
44

55
# ' @importFrom tidyselect where
6-
#' @importFrom dplyr mutate across
6+
#' @importFrom dplyr mutate across group_keys group_map
7+
#' @importFrom tibble tibble
78
#' @rdname dt_tools
89
#' @export
910
dt_round <- function(d, digits = 2) {
@@ -40,7 +41,7 @@ make_dt <- function(..., ncol = 3) {
4041
}) %>% do.call(rbind, .)
4142
}
4243

43-
#' @importFrom dplyr cur_group_id is_grouped_df
44+
#' @importFrom dplyr cur_group_id is_grouped_df mutate
4445
#' @export
4546
add_group_id <- function(d) {
4647
if (is_grouped_df(d)) {
@@ -50,8 +51,73 @@ add_group_id <- function(d) {
5051
}
5152
}
5253

53-
# tribble(
54-
# ~x, ~y,
55-
# "a", 1:3,
56-
# "b", 4:6
57-
# )
54+
#' grouped_list
55+
#'
56+
#' @param data A list object, `length(data) = nrow(group)`, with elements in the
57+
#' same order as group rows
58+
#' @param group A data.frame or data.table object
59+
#'
60+
#' @export
61+
grouped_list <- function(data, group) {
62+
R <- tibble(data, group)
63+
set_class(R, c("grouped_list", "tbl_df", "tbl", "data.frame"))
64+
}
65+
66+
#' grouped_list
67+
#'
68+
#' @export
69+
group_map2 <- function(df, .f, result.name = "data", ..., .keep = FALSE, .progress = FALSE) {
70+
group <- group_keys(df) %>% add_group_id()
71+
.f <- dplyr:::as_group_map_function(.f)
72+
73+
if (.progress) {
74+
n <- nrow(group)
75+
pb <- make_progress(n)
76+
fun <- function(.x, .y, ...) {
77+
pb$tick() # 更新进度条
78+
.f(.x, .y, ...)
79+
}
80+
} else {
81+
fun <- .f
82+
}
83+
data <- group_map(df, fun, ..., .keep = .keep)
84+
R <- tibble(!!result.name := data, group)
85+
set_class(R, c("grouped_list", "tbl_df", "tbl", "data.frame"))
86+
}
87+
88+
89+
#' @export
90+
export_fst <- function(x, ...) UseMethod("export_fst")
91+
92+
#' @importFrom fst write_fst read_fst
93+
#' @export
94+
export_fst.data.frame <- function(x, path, compress = 100, uniform_encoding = TRUE) {
95+
write_fst(x, path, compress, uniform_encoding)
96+
}
97+
98+
#' @export
99+
export_fst.grouped_list <- function(x, path, compress = 100, uniform_encoding = TRUE) {
100+
fcsv <- gsub(".fst", "_group.csv", path)
101+
fwrite(x[, -1], fcsv)
102+
103+
df <- x[[1]] %>% melt_list("I") # first column is data
104+
write_fst(df, path, compress, uniform_encoding)
105+
}
106+
107+
108+
#' @export
109+
import_fst <- function(
110+
path, columns = NULL, from = 1, to = NULL,
111+
as.data.table = TRUE, old_format = FALSE) {
112+
fcsv <- gsub(".fst", "_group.csv", path)
113+
data <- read_fst(path, columns, from, to, as.data.table, old_format)
114+
if (as.data.table) data = data.table(data)
115+
116+
if (file.exists(fcsv)) {
117+
group <- fread(fcsv)
118+
lst <- split(select(data, -I), data$I)
119+
grouped_list(lst, group)
120+
} else {
121+
data
122+
}
123+
}

R/reexports.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ qs::qsavem
4848
#' @export
4949
qs::qload
5050

51-
5251
#' @export
5352
qs::qsave
5453

5554
#' @export
5655
qs::qread
5756

5857

58+
5959
#' @export
6060
dplyr::select
6161

@@ -80,6 +80,13 @@ dplyr::as_tibble
8080
#' @export
8181
dplyr::tribble
8282

83+
#' @export
84+
dplyr::group_by
85+
86+
#' @export
87+
dplyr::group_map
88+
89+
8390
#' @export
8491
magrittr::`%>%`
8592

R/rio.R

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
#' @importFrom fst write_fst read_fst
2-
export_fst <- function(x, path, compress = 100, uniform_encoding = TRUE) {
3-
write_fst(x, path, compress, uniform_encoding)
4-
}
5-
6-
import_fst <- function(
7-
path, columns = NULL, from = 1, to = NULL,
8-
as.data.table = TRUE, old_format = FALSE) {
9-
read_fst(path, columns, from, to, as.data.table, old_format)
10-
}
11-
12-
131
#' export data
142
#' @details Support rda, rds, fst, csv, qs
153
#' @export
@@ -32,6 +20,7 @@ export <- function(x, path, ..., nthreads = 6) {
3220
}
3321
}
3422

23+
3524
#' import data to R
3625
#' @details Support rda, rds, fst, csv, qs
3726
#' @inheritParams qs::qread

man/add_gridLine.Rd

Lines changed: 0 additions & 16 deletions
This file was deleted.

man/group_map2.Rd

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/grouped_list.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/reexports.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)