Closed ThoDuyNguyen closed 6 years ago
I changed the code a little bit and new version works fine
calculate_retetention <-
function(f_read_new,
f_read_a1,
processing_date,
index_day,
checking_cohort,
file_name_summary) {
v_a1 <- f_read_a1(ymd(processing_date))
l_cohort <- lapply(checking_cohort, f_read_new)
v_count_cohort <- unlist(lapply(l_cohort, length))
l_remaining <- mapply(
base::intersect,
MoreArgs = list(x = v_a1),
y = l_cohort,
SIMPLIFY = FALSE
)
v_count_remaining <- unlist(lapply(l_remaining, length))
fwrite(
data.table(
cohort_date = checking_cohort,
day_index = index_day,
cohort_size = v_count_cohort,
remaining_cohort = v_count_remaining,
retention_rate = round(100*v_count_remaining / v_count_cohort, 3)
),
file_name_summary,
col.names = FALSE,
sep = "\t",
append = TRUE
)
}
running_date <- as.list(seq(from = ymd(20180501),
to = ymd(20180630),
by = 1))
file_name_summary <- "test_retention.tsv"
running_period <- 4
runninng_cohort_date <- mapply(get_cohort_date ,
processing_date = running_date,
period = running_period,
SIMPLIFY = FALSE)
running_index_day <- seq(from = 1, to = running_period, by = 1)
future_mapply(
calculate_retetention,
MoreArgs = list(
f_read_new = read_new,
f_read_a1 = read_a1,
file_name_summary = file_name_summary,
index_day = running_index_day
),
processing_date = running_date,
checking_cohort = runninng_cohort_date,
SIMPLIFY = FALSE
)
Could you please to explain the reason caused first version of code not working.
Kind regards
Thanks for the report, this looks like a bug in future_mapply()
. Here's a minimal example:
library(future.apply)
X <- as.Date("2018-06-01")
y0 <- mapply(FUN = identity, X, SIMPLIFY = FALSE)
str(y0)
# List of 1
# $ : Date[1:1], format: "2018-06-01"
y1 <- future_mapply(FUN = identity, X, SIMPLIFY = FALSE)
str(y1)
# List of 1
# $ : num 17683
This is because future_mapply()
subsets X
internally using:
x <- .subset(X, 1L)
str(x)
# num 17683
which does not support Date object. It should use
x <- X[1L]
str(x)
# Date[1:1], format: "2018-06-01"
instead.
Fixed in the develop branch which contains the next release. To install develop already, see the README.
FYI, future.apply 1.0.1, where this is fixed, is now on CRAN. Thxs again for the report.
read_new
andread_a1
is dummy function for testing purposeThis code throws:
Error in seq.int(0, to0 - from, by) : 'to' must be a finite number
But if i used
mapply
instead offuture_mapply
, the code is running well.Would you please to let me know what the problem is and how to fix it?
Thank you in advance.
My session info