Skip to content

Commit d58f6b1

Browse files
committed
use withr::defer instead of on.exit to use sequental plan on function exit
1 parent 2dbfd1b commit d58f6b1

18 files changed

+24
-24
lines changed

R/DWF_CHELSA_prepare.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ CHELSA_prepare <- function(
206206
c1 <- snow::makeSOCKcluster(n_cores)
207207
on.exit(try(snow::stopCluster(c1), silent = TRUE), add = TRUE)
208208
future::plan("future::cluster", workers = c1, gc = TRUE)
209-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
209+
withr::defer(future::plan("future::sequential", gc = TRUE))
210210
}
211211

212212
# # |||||||||||||||||||||||||||||||| ###

R/DWF_CHELSA_process.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ CHELSA_process <- function(
166166
c1 <- snow::makeSOCKcluster(n_cores)
167167
on.exit(try(snow::stopCluster(c1), silent = TRUE), add = TRUE)
168168
future::plan("future::cluster", workers = c1, gc = TRUE)
169-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
169+
withr::defer(future::plan("future::sequential", gc = TRUE))
170170
}
171171

172172
CHELSA_Data_Checked <- CHELSA_Data %>%
@@ -232,7 +232,7 @@ CHELSA_process <- function(
232232
c1 <- snow::makeSOCKcluster(n_cores)
233233
on.exit(try(snow::stopCluster(c1), silent = TRUE), add = TRUE)
234234
future::plan("future::cluster", workers = c1, gc = TRUE)
235-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
235+
withr::defer(future::plan("future::sequential", gc = TRUE))
236236
}
237237

238238

@@ -371,7 +371,7 @@ CHELSA_process <- function(
371371
c1 <- snow::makeSOCKcluster(n_cores)
372372
on.exit(try(snow::stopCluster(c1), silent = TRUE), add = TRUE)
373373
future::plan("future::cluster", workers = c1, gc = TRUE)
374-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
374+
withr::defer(future::plan("future::sequential", gc = TRUE))
375375
}
376376

377377
# String to be matched to extract variable names

R/DWF_EASIN_process.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ EASIN_process <- function(
276276
c1 <- snow::makeSOCKcluster(n_cores)
277277
on.exit(try(snow::stopCluster(c1), silent = TRUE), add = TRUE)
278278
future::plan("future::cluster", workers = c1, gc = TRUE)
279-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
279+
withr::defer(future::plan("future::sequential", gc = TRUE))
280280
}
281281

282282
IASDT.R::cat_time("Processing EASIN data", level = 1)

R/DWF_Efforts_Summarize.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ efforts_summarize <- function(
8686
c1 <- snow::makeSOCKcluster(n_cores)
8787
on.exit(try(snow::stopCluster(c1), silent = TRUE), add = TRUE)
8888
future::plan("future::cluster", workers = c1, gc = TRUE)
89-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
89+
withr::defer(future::plan("future::sequential", gc = TRUE))
9090
}
9191

9292
# # ..................................................................... ###

R/DWF_GBIF_process.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ GBIF_process <- function(
205205
invisible()
206206

207207
future::plan("future::cluster", workers = c1, gc = TRUE)
208-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
208+
withr::defer(future::plan("future::sequential", gc = TRUE))
209209
}
210210

211211
IASDT.R::cat_time(
@@ -604,7 +604,7 @@ GBIF_process <- function(
604604
c1 <- snow::makeSOCKcluster(n_cores)
605605
on.exit(try(snow::stopCluster(c1), silent = TRUE), add = TRUE)
606606
future::plan("future::cluster", workers = c1, gc = TRUE)
607-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
607+
withr::defer(future::plan("future::sequential", gc = TRUE))
608608
}
609609

610610
IASDT.R::cat_time("Splitting species data on parallel", level = 2)

R/DWF_IAS_process.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ IAS_process <- function(env_file = ".env", n_cores = 6L, overwrite = TRUE) {
180180
c1 <- snow::makeSOCKcluster(n_cores)
181181
on.exit(try(snow::stopCluster(c1), silent = TRUE), add = TRUE)
182182
future::plan("future::cluster", workers = c1, gc = TRUE)
183-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
183+
withr::defer(future::plan("future::sequential", gc = TRUE))
184184
}
185185

186186
# # .................................... ###

R/DWF_efforts_download.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ efforts_download <- function(n_cores = 6L, env_file = ".env") {
7272
c1 <- snow::makeSOCKcluster(n_cores)
7373
on.exit(try(snow::stopCluster(c1), silent = TRUE), add = TRUE)
7474
future::plan("future::cluster", workers = c1, gc = TRUE)
75-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
75+
withr::defer(future::plan("future::sequential", gc = TRUE))
7676
}
7777

7878
# # ..................................................................... ###

R/DWF_efforts_request.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ efforts_request <- function(
7878
c1 <- snow::makeSOCKcluster(min(n_cores, 3))
7979
on.exit(try(snow::stopCluster(c1), silent = TRUE), add = TRUE)
8080
future::plan("future::cluster", workers = c1, gc = TRUE)
81-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
81+
withr::defer(future::plan("future::sequential", gc = TRUE))
8282
}
8383

8484
# # ..................................................................... ###

R/DWF_railway_intensity.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ railway_intensity <- function(
261261
c1 <- snow::makeSOCKcluster(n_cores)
262262
on.exit(try(snow::stopCluster(c1), silent = TRUE), add = TRUE)
263263
future::plan("future::cluster", workers = c1, gc = TRUE)
264-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
264+
withr::defer(future::plan("future::sequential", gc = TRUE))
265265
}
266266

267267
## Processing railway data ----

R/mod_convergence_plot.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ convergence_plot <- function(
626626

627627
# Prepare working on parallel
628628
IASDT.R::set_parallel(n_cores = min(n_cores, nrow(Beta_DF)), level = 2)
629-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
629+
withr::defer(future::plan("future::sequential", gc = TRUE))
630630

631631
# # |||||||||||||||||||||||||||||||||||||||||||||||||||||||| ##
632632

@@ -797,7 +797,7 @@ convergence_plot <- function(
797797
# Prepare working on parallel
798798
IASDT.R::set_parallel(
799799
n_cores = min(n_cores, nrow(BetaTracePlots_ByVar)), level = 2)
800-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
800+
withr::defer(future::plan("future::sequential", gc = TRUE))
801801

802802
# # |||||||||||||||||||||||||||||||||||||||||||||||||||||||| ##
803803

@@ -925,7 +925,7 @@ convergence_plot <- function(
925925
# Prepare working on parallel
926926
IASDT.R::set_parallel(
927927
n_cores = min(n_cores, nrow(BetaTracePlots_BySp)), level = 2)
928-
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
928+
withr::defer(future::plan("future::sequential", gc = TRUE))
929929

930930
# # |||||||||||||||||||||||||||||||||||||||||||||||||||||||| ##
931931

0 commit comments

Comments
 (0)