Closed ThoDuyNguyen closed 5 years ago
Very quick reply: Could this be related to the 'data.table' issue described in https://cran.r-project.org/web/packages/future/vignettes/future-4-issues.html. If so, try with:
l_df_test <- future_lapply(l_df, function(x){return(x)}, future.packages = "data.table")
FYI, your very first example gives an error in a fresh R session:
> library(data.table)
data.table 1.11.4 Latest news: http://r-datatable.com
> add_new_column <-
+ function(df, init_value) {
+ df[, new_column = init_value]
+ }
>
> l_df <-
+ list(as.data.table(iris),
+ as.data.table(iris),
+ as.data.table(iris))
>
> mapply(
+ add_new_column,
+ df = l_df,
+ init_value = 1:3,
+ SIMPLIFY = FALSE
+ )
Error in `[.data.table`(df, , new_column = init_value) :
unused argument (new_column = init_value)
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.11.4
loaded via a namespace (and not attached):
[1] compiler_3.5.1
I forgot the bug for a while
My code should be
add_new_column <-
function(df, init_value) {
df[, new_column := init_value]
}
l_df <-
list(as.data.table(iris),
as.data.table(iris),
as.data.table(iris))
mapply(
add_new_column,
df = l_df,
init_value = 1:3,
SIMPLIFY = FALSE
)
It works for me; what's the problem?
library(future.apply)
plan(multisession, workers = 2)
library(data.table)
add_new_column <-
function(df, init_value) {
df[, new_column := init_value]
}
l_df <-
list(as.data.table(iris),
as.data.table(iris),
as.data.table(iris))
y0 <- mapply(
add_new_column,
df = l_df,
init_value = 1:3,
SIMPLIFY = FALSE
)
y1 <- future_mapply(
add_new_column,
df = l_df,
init_value = 1:3,
SIMPLIFY = FALSE
)
print(all.equal(y1, y0))
### [1] TRUE
with
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.11.4 future.apply_1.0.1 future_1.9.0
loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1 parallel_3.5.1 listenv_0.7.0-9000
[5] codetools_0.2-15 digest_0.6.17 globals_0.12.3
Don't mind my listenv_0.7.0-9000 - it works also with listenv_0.7.0
Did you see my follow up? Can we close?
Didn't hear back from you. I'll assume this if resolved, but feel free to reopen if not the case.
I want to apply a function to a list of data.table for side effect. My example code works perfectly
And the result is correct:
But if my list is a result of a future_lapply for example:
When I make a mapply to this list, reference semantic is lost:
My session info: