In the code below, you can see that Sys.Date() - months(1)
works, but not Sys.Date() - months(i)
within a foreach parallel loop. Why won't this work? Is it possible to make it work?
library(tidytable)
#>
#> Attaching package: 'tidytable'
#> The following object is masked from 'package:stats':
#>
#> dt
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library(foreach)
# Set multi core parallel
cl = parallel::makePSOCKcluster(12)
doParallel::registerDoParallel(cl)
# WORKS
Sys.Date() - months(1)
#> [1] "2022-09-20"
df_output = foreach(i = 1:10,
.combine = 'rbind') %dopar% {
# DOES NOT WORK
date_anything = Sys.Date() - months(i)
df = data.frame(abc = i,
def = 123)
}
#> Error in {: task 1 failed - "no applicable method for 'months' applied to an object of class "c('integer', 'numeric')""
Created on 2022-10-20 by the reprex package (v2.0.1)