I am developing an R package that utilizes S4 classes and methods. In one of the methods, I am using mclapply
from the parallel
package to process data in parallel. This method has an if-else nest that checks a string argument and runs different internal functions accordingly.
The problem is that when I build the package and try to run the method, I get an error that the string value object doesn't exist, even though it's passed as a string. Surprisingly, when I use lapply
instead of mclapply
, everything works fine.
Here's a simplified example to illustrate my issue:
# External
out_function <- function(input_vector, condition_string="add_2") {
result_list <-
parallel::mclapply(input_vector,
function(i, condition_string_lapply=condition_string) {
if (condition_string == "add_2") {
i <- in_add_2(i)
} else if(condition_string == "add_3") {
i <- in_add_3(i)
}
}, mc.cores=3)
result <- unlist(result_list)
return(result)
}
# Internal
in_add_2 <- function(i) {
return(i + 2)
}
# Internal
in_add_3 <- function(i) {
return(i + 3)
}
final_result <- out_function(input_vector=seq(1, 10, 1),
condition_string="add_3")
Note: out_function
is a public method in my package, while in_add_2
and in_add_3
are internal functions.
Error:
The error message I get is: "add_2" object not found
I also tried debugging with debug()
, and it says in_add_2
missing.
I am kind of confused about what I am doing wrong. How can I fix this issue so that mclapply
recognizes the string as a string argument, not an object? Or is this related to the internal functions somehow?
Thank you for any help on this matter!
session info
> sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=es_ES.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=es_ES.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=es_ES.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C
time zone: Europe/Madrid
tzcode source: system (glibc)
attached base packages:
[1] parallel stats4 stats graphics grDevices utils datasets
[8] methods base
other attached packages:
[1] iCOBRA_1.28.0 DESeq2_1.40.1
[3] assertthat_0.2.1 Matrix_1.5-1
[5] sctransform_0.3.5 SeuratObject_4.1.3
[7] Seurat_4.3.0 gtools_3.9.4
[9] tradeSeq_1.14.0 my_package_1.0.2
[11] lubridate_1.9.2 forcats_1.0.0
[13] stringr_1.5.0 dplyr_1.1.3
[15] purrr_1.0.2 readr_2.1.4
[17] tidyr_1.3.0 tibble_3.2.1
[19] ggplot2_3.4.3 tidyverse_2.0.0
[21] SingleCellExperiment_1.22.0 SummarizedExperiment_1.30.1
[23] Biobase_2.60.0 GenomicRanges_1.52.0
[25] GenomeInfoDb_1.36.0 IRanges_2.34.0
[27] S4Vectors_0.38.1 BiocGenerics_0.46.0
[29] MatrixGenerics_1.12.0 matrixStats_0.63.0
loaded via a namespace (and not attached):
[1] RcppAnnoy_0.0.20 TrajectoryUtils_1.8.0 slingshot_2.8.0
[4] splines_4.3.0 later_1.3.1 bitops_1.0-7
[7] polyclip_1.10-4 lifecycle_1.0.3 rstatix_0.7.2
[10] edgeR_3.42.2 doParallel_1.0.17 globals_0.16.2
[13] lattice_0.21-8 MASS_7.3-59 backports_1.4.1
[16] magrittr_2.0.3 limma_3.56.1 plotly_4.10.1
[19] shinyBS_0.61.1 httpuv_1.6.11 spatstat.sparse_3.0-1
[22] sp_1.6-0 reticulate_1.28 cowplot_1.1.1
[25] pbapply_1.7-0 minqa_1.2.5 RColorBrewer_1.1-3
[28] abind_1.4-5 zlibbioc_1.46.0 Rtsne_0.16
[31] RCurl_1.98-1.12 monocle3_1.3.1 GenomeInfoDbData_1.2.10
[34] ggrepel_0.9.3 irlba_2.3.5.1 spatstat.utils_3.0-3
[37] listenv_0.9.0 terra_1.7-29 moments_0.14.1
[40] venn_1.11 goftest_1.2-3 spatstat.random_3.1-5
[43] fitdistrplus_1.1-11 parallelly_1.35.0 leiden_0.4.3
[46] codetools_0.2-19 DelayedArray_0.26.3 DT_0.28
[49] tidyselect_1.2.0 farver_2.1.1 lme4_1.1-33
[52] viridis_0.6.3 spatstat.explore_3.2-1 jsonlite_1.8.7
[55] ellipsis_0.3.2 progressr_0.13.0 ggridges_0.5.4
[58] survival_3.5-5 iterators_1.0.14 RColorConesa_1.0.0
[61] foreach_1.5.2 tools_4.3.0 ica_1.0-3
[64] Rcpp_1.0.11 glue_1.6.2 gridExtra_2.3
[67] mgcv_1.8-42 admisc_0.32 shinydashboard_0.7.2
[70] withr_2.5.1 fastmap_1.1.1 boot_1.3-28
[73] fansi_1.0.5 entropy_1.3.1 digest_0.6.33
[76] timechange_0.2.0 R6_2.5.1 mime_0.12
[79] colorspace_2.1-0 scattermore_1.1 tensor_1.5
[82] spatstat.data_3.0-1 UpSetR_1.4.0 utf8_1.2.3
[85] generics_0.1.3 data.table_1.14.8 httr_1.4.6
[88] htmlwidgets_1.6.2 S4Arrays_1.0.4 uwot_0.1.14
[91] pkgconfig_2.0.3 gtable_0.3.4 lmtest_0.9-40
[94] XVector_0.40.0 htmltools_0.5.5 carData_3.0-5
[97] scales_1.2.1 png_0.1-8 rstudioapi_0.14
[100] tzdb_0.4.0 reshape2_1.4.4 nlme_3.1-162
[103] nloptr_2.0.3 zoo_1.8-12 KernSmooth_2.23-20
[106] miniUI_0.1.1.1 pillar_1.9.0 grid_4.3.0
[109] vctrs_0.6.3 RANN_2.6.1 promises_1.2.0.1
[112] ggpubr_0.6.0 car_3.1-2 xtable_1.8-4
[115] cluster_2.1.4 princurve_2.1.6 cli_3.6.1
[118] locfit_1.5-9.7 compiler_4.3.0 rlang_1.1.1
[121] crayon_1.5.2 future.apply_1.10.0 ggsignif_0.6.4
[124] labeling_0.4.3 mclust_6.0.0 plyr_1.8.8
[127] stringi_1.7.12 deldir_1.0-9 viridisLite_0.4.2
[130] BiocParallel_1.34.1 munsell_0.5.0 lazyeval_0.2.2
[133] spatstat.geom_3.2-1 hms_1.1.3 patchwork_1.1.2
[136] future_1.32.0 shiny_1.7.4 ROCR_1.0-11
[139] igraph_1.4.2 broom_1.0.4