Search code examples
rr-futurecallr

When performing multisession work, future_lapply says that a package doesn't exist, but it works fine when running plan(sequential)


When I try to use future_apply with plan(multisession), it says that the package I'm trying to use doesn't exist. When I use plan(sequential) it works fine. I also get the same error when using plan(callr).

Here's the error:

Error in loadNamespace(name): there is no package called 'fuzzyjoin'

Can anyone help me figure out a solution or what's going wrong here?

I'm not sure if this is related to the future.apply package or future or globals packages as I know that they are also involved here.

Here's my code showing the issue:

library(fuzzyjoin)
library(future.apply)
#> Loading required package: future
library(dplyr)
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)


iris_mod<- iris %>%
  mutate(examplefield= Sepal.Width + Petal.Length,
         Species = as.character(Species))


iristype <- iris_mod$Species %>% unique()

plan(sequential)

test_sequential <- future_lapply(iristype, 
                               FUN = function(x) {
                                 fuzzyjoin::fuzzy_left_join(
                                   iris_mod %>% filter(Species %in% x),
                                    iris_mod, 
                                    by = c("Species"="Species",
                                           "examplefield"="Sepal.Length"),
                                    match_fun = list(`==`, `<`)
                                 )},
                               future.chunk.size= 2
)


plan(multisession)

test_multisession <- future_lapply(iristype, 
                                   FUN = function(x) {
                                     fuzzyjoin::fuzzy_left_join(
                                       iris_mod %>% filter(Species %in% x),
                                        iris_mod, 
                                        by = c("Species"="Species",
                                               "examplefield"="Sepal.Length"),
                                        match_fun = list(`==`, `<`)
                                     )},
                                   future.chunk.size=2
)
#> Error in loadNamespace(name): there is no package called 'fuzzyjoin'

Created on 2022-01-28 by the reprex package (v2.0.1)

I'm running R v4.0.3 if that's relevant.


Solution

  • I ran the following code and found that the library paths weren't being passed correctly for some reason. My dirty fix was just to make sure the packages were installed on the libPath where future was looking.

    install.packages("fuzzyjoin", lib= "C:/Program Files/R/R-4.0.3/library" )
    

    Here's the code that I ran to discover my normal session and future_lapply/future session were using different library paths:

    .libPaths()
    # [1] "\\\\networkfileservername/Userdata/myusername/Home/R/win-library/4.0" "C:/Program Files/R/R-4.0.3/library"    
    
    
    f_libs%<-% .libPaths()
    print(f_libs)
    # [1] "C:/Program Files/R/R-4.0.3/library"