Search code examples
rforeachdoparallelterra

NULL value passed as symbol address error in foreach loop R


I have never had problems with this before, but I am running into this error when trying to use a foreach loop in R: "Error in { : task 1 failed - "NULL value passed as symbol address".

It is next to impossible for me to produce a small, reproducible example of this (I've tried!) since I am trying to extract data from huge rasters and make csv files from that data. But, here's my code.

bi_2021 <- rast('G:\\GridMet_Yearly\\bi_2021.nc')

cl <- makeCluster(2)
registerDoParallel(cl)

r = 1
foreach (r=1:10, .packages = c('tidyverse','lubridate')) %dopar% {
  rc <- row_char[r]
  cc <- col_char[r]
  ce <- cell_char[r]
  rn <- row_num[r]
  cn <- col_num[r]
  fname <- paste0('G:/GridMet_Cells_RawData/row',rc,'_col',cc,'_cell',ce,'.csv')

  data_df <- data.frame(read_csv(fname, show_col_types = FALSE)) # read previous data in
  data_df <- data[which(year(data$Date) < 2021),]

  # add rows for 2021 daily data
  data_df[15342:15673,] <- NA
  data_df$Date[15342:15673] <- seq(as.Date('2021-01-01'),as.Date('2021-11-28'),'days')
  data_df$bi[15342:15673] <- as.numeric(bi_2021[rn,cn,][1:332])  # THIS IS THE LINE IT DOESN'T                                                                                       
                                                                         LIKE!

  write_csv(final_df,paste0('G://GridMet_Cells_RawData2//row',rc,'_col',cc,'_cell',ce,'.csv'))
  rm(data_df,cc,ce,cn,fname,rc,rn)}

Here's a sample of the data.

  Date          bi   erc etr_alfalfa fm100 fm1000 etr_grass    pp rhmax rhmin  shum  srad    ud  tmin  tmax   vpd    us
  <date>     <dbl> <dbl>       <dbl> <dbl>  <dbl>     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 1979-01-01    22    16         0.1  18.2   24.5       0.1   0    74.4  38.7  0.14  78.1   232 -40.8 -26.8    20   2.5

For some reason it's not letting me extract the data from the raster, and put it in the specific rows for each variable. Any ideas as to why it would be doing this and how to fix it? It works fine when it's not put inside a foreach loop. Thanks.


Solution

  • You cannot directly use a SpatRaster in parallelization. See this github issue. Some methods have built in support (predict, app, lapp and tapp) and there are different ways to approach this in other cases (perhaps see this issue).

    For example, it may work if you use this line

    bi_2021 <- rast('G:\\GridMet_Yearly\\bi_2021.nc')
    

    inside the foreach loop.