Last active
September 9, 2019 00:31
-
-
Save stemangiola/dd3573be22492fc03856cd2c53a755a9 to your computer and use it in GitHub Desktop.
This is a collection of custom tidy functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
future::plan(future:::multiprocess) | |
do_and_return = function(tbl, code){ | |
# Execute future | |
dummy = future::future(code()) | |
# Return | |
tbl | |
} | |
do_parallel_start = function(df, .f, ...){ | |
detect_cores = function(){ | |
if(.Platform$OS.type == "unix") | |
system("nproc", intern = TRUE) %>% as.integer %>% sum(-1) | |
else if(.Platform$OS.type == "windows") | |
parallel::detectCores() %>% as.integer %>% sum(-1) | |
else stop("Your platform type is not recognised") | |
} | |
cores = detect_cores() | |
cl <- parallel::makeCluster(cores) | |
parallel::clusterExport(cl, lsf.str(envir = parent.env(environment()))) | |
parallel::clusterExport(cl, ".f", envir = environment()) | |
#parallel::clusterEvalQ(cl, .f) | |
parallel::clusterEvalQ(cl, { | |
library(tidyverse) | |
library(magrittr) | |
}) | |
partition_col = enquos(...) | |
df %>% | |
left_join( | |
(.) %>% | |
distinct(!!partition_col[[1]]) %>% | |
mutate( | |
part = 1:n() %>% | |
magrittr::divide_by(length((.))) %>% | |
magrittr::multiply_by(!!cores) %>% | |
ceiling | |
) | |
) %>% | |
#mutate(`.f` = .f %>% list) %>% | |
multidplyr::partition(part, cluster = cl) %>% | |
do({ | |
#.f = (.) %>% slice(1) %>% pull(`.f`) %>% `[[` (1) | |
(.) %>% | |
group_by(!!!partition_col) %>% | |
do({ | |
purrr::as_mapper(.f)( (.) ) | |
}) | |
#purrr::as_mapper(.f)((.) %>% group_by(!!!partition_col)) | |
}) %>% | |
collect() %>% | |
ungroup() %>% | |
select(-part) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment